JAX-RS为XML类型响应的GET操作提供了状态代码500



我刚刚开始学习RESTful服务,这是我最近面临的问题。当我运行应用程序以获取XML类型响应时,我会得到状态代码500,并且我也看不到任何堆栈跟踪。我在依赖项列表中遗漏了什么吗?请帮我计算一下。

这是实体类:


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 + "]";
}
}

然后我有了我的服务类: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;
}
}

然后我有了资源类: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();
}
}

下面是我的pom.xml的样子:

<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>

请帮我弄清楚。谢谢

Sam建议的链接回答了您的问题。您必须将响应包装为GenericEntity。以下是响应类的文档:

"如果需要保留其泛型类型,则调用者有责任用GenericEntity包装实际实体。">

所以你的代码应该是这样的:

@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(); ;
}

相关内容

  • 没有找到相关文章