Creating an restful application but it is not returning the response in XML. Even there is no log on the console when hitting the URL "http://localhost:8080/message/webapi/messages".
I am returning a list and using @Produces(MediaType.APPLICATION_XML) to return the response in XML.
MessageResource.java
package org.porwal.restful.message.resources;
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 org.porwal.restful.message.model.Message;
import org.porwal.restful.message.service.MessageService;
@Path("/messages")
public class MessageResource {
MessageService ms = new MessageService();
@GET
@Produces(MediaType.APPLICATION_XML)
public List<Message> getMessage(){
return ms.getAllMessage();
}
}
Message.java
package org.porwal.restful.message.model;
import java.util.Date;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement( name = "Message" )
public class Message {
public long id;
public String message;
public Date created;
public String author;
public Message() {
}
public Message(long id, String message, String author) {
this.id = id;
this.message = message;
this.author = author;
this.created = new Date();
}
public long getId() {
return id;
}
@XmlElement (name = "ID")
public void setId(long id) {
this.id = id;
}
public String getMessage() {
return message;
}
@XmlElement (name = "Message")
public void setMessage(String message) {
this.message = message;
}
public Date getCreated() {
return created;
}
@XmlElement (name = "Created")
public void setCreated(Date created) {
this.created = created;
}
public String getAuthor() {
return author;
}
@XmlElement (name = "Author")
public void setAuthor(String author) {
this.author = author;
}
}
This is working if i do not use @XMLRootElement annotation and TEXT_PLAIN is returned well through the URL. I also tried to remove @XmlElement for each fields but no luck. When i remove @XMLRootElement then MessageBodyWriter error can be seen in logs on eclipse console but when includes @XMLRootElement then no logs on eclipse console and URL "http://localhost:8080/message/webapi/messages" throws the error:
Error in case of @XmlRootElement is missing.
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=java.util.List<org.porwal.restful.message.model.Message>. This exception comes only when i commented the line "//@XmlRootElement( name = "Message" )".
HTTP Status 500 – Internal Server Error
Can someone please tell what i am missing here?