Professional Documents
Culture Documents
001 Getting List of Messages
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
public Message(){
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;
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 {
@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();
}}