Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

003 GETTING A SINGLE RESOURCE

1. Our Pojo class as it is

package com.module.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

//we need to convert the list into and XML Format presentation... how we will do that.
//simple we will tell our RESTEasy that hey..there is already JAXP api with you, which
will convert this to XML...use it...
//But this JAXP needs a clue what to convert in XML???
//for this use @XxmlRootElement like below..
//if you are not using this chances are of getting Runtime Exception Saying "COULD NOT
FIND MESSAGEBODYWIRTER"
@XmlRootElement
public class Message {

//a POJO class with getters and setters method to get one instance of message
private long id;
private String messages;
private Date created;
private String author;

//ALWAYS ENSURE THAT MODEL CLASSES HAVE A NO-ARGUMENT CONSTRUCTOR


//bcoz wenever we are messing up arround xml or JSON we need no-args
constructor
//we need a way for those frameworks to create new instances of your class
public Message(){

}
//to create instances of message class..we defined following constructor
//whenever you are creating a constructor like below...always...always make sure
that you have basic no args constructor...like above
public Message(long id, String messages, String author){
this.id=id;
this.messages=messages;
this.author=author;
this.created=new Date();

}
//and all getters and setters for the variables
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getMessages() {
return messages;
}
public void setMessages(String messages) {
this.messages = messages;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}

// so this is the class which contains one instance of Message


// now next is we need a service that returns list of messages

2. Our MessageService class as it is

package com.module.service;

import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.module.database.SampleDatabase;
import com.module.model.Message;

public class MessageService {

//we created a local member varibale which is map of long and message and getting
messages from SampleDatabase..SampleDatabase.getMessages();
private static Map<Long,Message> messages=SampleDatabase.getMessages();

public MessageService(){
//creating a new messages with ID=> 1L for HashMap<Long, Message>
messages.put(1L,new Message(1,"GOOD MORNING","GITESH MORE"));
messages.put(2L,new Message(2, "GOOD AFTERNOON","SHAILESH"));
messages.put(3L,new Message(3,"GOOD EVENING","KIRAN"));
}
// now next is we need a service that returns list of messages
public List<Message> getAllMessages(){
//deleted previous stuff
//we are going to have list of all messages...and this we want as list so done
as follows
return new ArrayList<Message>(messages.values());
//passing a collection to the arraylist constructor initialized List with those
elements
}

//to get only one message


public Message getMessage(long id){
return messages.get(id);
}

//to add message


public Message addmessage(Message message){
message.setId(messages.size()+1);
messages.put(message.getId(), message);
return message;
}

//to update message


public Message updateMessage(Message message){
if(message.getId()<=0){
return null;
}
messages.put(message.getId(), message);
return message;
}

//to remove message


public Message removeMessage(long id){
return messages.remove(id);
}
}

3. Our Profile class also as it is..as we are not yet dealing with it
package com.module.model;
//this class is created for 003 docs...message service stub
import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Profile {

private long id;


private String profileName;
private String firstName;
private String lastName;
private Date created;

public Profile(){

public Profile(long id,String profileName, String firstName,String lastName){


this.id=id;
this.profileName=profileName;
this.firstName=firstName;
this.lastName=lastName;
this.created=new Date();
}

public long getId() {


return id;
}
public void setId(long id) {
this.id = id;
}
public String getProfileName() {
return profileName;
}
public void setProfileName(String profileName) {
this.profileName = profileName;
}

public String getFirstName() {


return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}

4. Now as we are trying to access the message with ID.. so we are gone change the paths too..
So we will have to update in our MessageResource class as bellows

package com.module;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.module.model.Message;
import com.module.service.MessageService;

@Path("/messages")
public class MessageResource {

//2. for message service we need to create a new instance of


MessageService
MessageService messageService=new MessageService();

@GET
@Produces(MediaType.APPLICATION_XML)
//public String getMessages(){
//for a list reurn type
public List<Message> getMessages(){
//then instead of returning Hello Gitesh...welcome to resteasy
tutorial...we are going to return
return messageService.getAllMessages();

//but above method returns the list of messages and it is refault resource
URI for this class.
//what if we want to access single resource??
//how can we do that ??
//it would be like following
//RESTEasy allows you to use path annotation even for this...
@GET
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
public String test1(){
return "test";
}

//http://localhost:8180/TEST_D_REST_GETSingleResource/messages... it is
a constant URI.. it will always take you to this class.
//http://localhost:8180/TEST_D_REST_GETSingleResource/messages/test
but this URI will take you the path of test inside that class..where test is a resource..
but its not a variable..
//so what about variable???
//if you want to use variable...declare it in {} in @Path annotation.

/*@GET
@Path("/{messageId}")
@Produces(MediaType.TEXT_PLAIN)
public String Variable(){
return "test";
}
*/

//now above method will take any variable and return the same value...
//but how about if we want it to be a resource specific..
//lets say when we passsed 1 in URI ... it should display only messages of
message id 1...

/*@GET
@Path("/{messageId}")
@Produces(MediaType.APPLICATION_XML)
public Message getPerticularMessage(String id){
//now we should pass the messageId to our message service class's
object to get the corresponding message

return messageService.getMessage(id);
}*/
//see above method (commented method)it is giving error saying that...
getMessage(id) takes long as a parameter but you are giving me String..
//it can't work..
//so one way is...you could convert that yourself from String to long...
//but RESTEasy can help you with that... it will automatically convert it for
you..how ??? BY USING @PathParam annotation
//in method below...
//RESTEasy is getting path parameter as a string.. but we said we want it
long id...
//so RESTEasy converted it for you
// @PathParam("messageId") => take the parameter from path with this
name....and @PathParam("messageId")long id => inject or parse it to long id
@GET
@Path("/{messageId}")
@Produces(MediaType.APPLICATION_XML)
public Message getPerticularMessage(@PathParam("messageId")long id){
return messageService.getMessage(id);
}

//now till here we have 3 APIs developed...

//1st API => to get all messages = >


http://localhost:8180/TEST_D_REST_GETSingleResource/messages
//2nd API => to get test = >
http://localhost:8180/TEST_D_REST_GETSingleResource/messages/test
//3rd API => to get message with perticular message id = >
http://localhost:8180/TEST_D_REST_GETSingleResource/messages/{messageId}

//now talking about 3rd API..


//if we pass URI like
http://localhost:8180/TEST_D_REST_GETSingleResource/messages/1 then we
will get message with id 1
//if we pass URI like
http://localhost:8180/TEST_D_REST_GETSingleResource/messages/2 then we
will get message with id 2
//if we pass URI like
http://localhost:8180/TEST_D_REST_GETSingleResource/messages/3 then we
will get message with id 3
//if we pass URI like
http://localhost:8180/TEST_D_REST_GETSingleResource/messages/4 then we
will get no body...that is we will get NULL... and status will be displayed as 204-No
Content
//it means that.. RESTEasy is saying us ... I have method to handle this kind
of requests..but for id with 4 I dont have contants to be displayed

//if we pass URI like


http://localhost:8180/TEST_D_REST_GETSingleResource/messages/abcdksdjc then
we will get 404... bcoz it is String and not long... so it would give us 404..as it
would be searching for where could I fit this..but it didn't found it.

5.

You might also like