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

001 GETTING LIST OF MESSAGES

1. Now we sent a basic PLAIN_TEXT in response… Let us try for a list…sending List of Messages

2. Let us create another class… which is a POJO Class with getters and setters and a constructor
to create the message

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

3. Now create another class MessageService which is going to provide message list

package com.module.service;

import java.util.ArrayList;
import java.util.List;

import com.module.model.Message;

public class MessageService {

//we have declared a POJO class to get one instance of message


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

public List<Message> getAllMessages(){


//for the following step we need constructor in Message
POJO class so that we can create the actual messages
Message m1=new Message(1L,"GOOD MORNING","GITESH MORE");
Message m2=new Message(1L,"GOOD AFTERNOON","SHAILESH");
Message m3=new Message(1L,"GOOD EVENING","KIRAN");

List<Message> list=new ArrayList<>();


list.add(m1);
list.add(m2);
list.add(m3);

return list;
}
}

4. Now to access this list our “MessageResource” class will have few changes like below

package com.module;

import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
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();

}}

You might also like