首页 > 解决方案 > 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>

请帮我解决这个问题。谢谢。

标签: javaxmlrestmaven

解决方案


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

“如果需要保留其泛型类型,调用者有责任用 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(); ;
    }

推荐阅读