Reputation: 535
I have just begun to learn RESTful services and this is the issue I have been facing recently. When I run the application to GET an XML type response, I get status code 500 and I don't see any stack trace for this as well. Am I missing anything in the dependency list ? Please help me figure this.
Here is the entity class :
import java.util.Date;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Message {
private int id;
private String message;
private String author;
private Date date;
public Message() {}
public Message(int id, String message, String author) {
super();
this.id = id;
this.message = message;
this.author = author;
this.date = new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "Message [id=" + id + ", message=" + message + ", author=" + author + ", date=" + date + "]";
}
}
And then I have my service class : MessageService.java :
import java.util.ArrayList;
import java.util.List;
import org.aravind.restful.messenger.model.Message;
public class MessageService {
public List<Message> getMessages()
{
Message m1 = new Message(1,"Hello World!","aravind");
Message m2 = new Message(2,"First step towards the goal!","aravind");
List <Message> messageList = new ArrayList<Message>();
messageList.add(m1);
messageList.add(m2);
return messageList;
}
}
And then I have the resource class : MessengerResouce.java
package org.aravind.restful.messenger.resources;
import java.util.List;
import org.aravind.restful.messenger.model.Message;
import org.aravind.restful.messenger.service.MessageService;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/messages")
public class MessengerResource {
@GET
@Produces(MediaType.APPLICATION_XML)
public List<Message> getMessages()
{
MessageService msgService = new MessageService();
return msgService.getMessages();
}
}
And here is how my pom.xml looks :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.aravind.restful</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>
<build>
<finalName>messenger</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>3.0.0-M1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Please help me figure this out. Thanks.
Upvotes: 0
Views: 448
Reputation: 51
The link Sam suggested answers your question. You have to wrap your response as a GenericEntity. Here is the documentation from the Response class:
"It is the callers responsibility to wrap the actual entity with GenericEntity if preservation of its generic type is required."
So your code should look something like this:
@GET
@Produces(MediaType.APPLICATION_XML)
public Response getMessages()
{
MessageService msgService = new MessageService();
return Response.status(Status.OK)
.entity(new GenericEntity<>(msgService.getMessages(), List.class))
.build(); ;
}
Upvotes: 1