【问题标题】:How to solve produce xml in java rest services with jersey?java - 如何使用jersey解决在java rest服务中生成xml?
【发布时间】:2015-09-03 07:41:44
【问题描述】:

我正在使用 jersey 创建简单的 Web 服务。

这里一切正常,但我无法以 xml 格式获取数据现在我使用 json 格式显示此 json 显示器的数据我添加了 jersey-media-moxy 依赖项,我不知道我需要添加任何xml依赖。

这是我的 pom.xml

<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>
    <!-- uncomment this to get JSON support -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

    <dependency>
        <groupId>net.vz.mongodb.jackson</groupId>
        <artifactId>mongo-jackson-mapper</artifactId>
        <version>1.4.2</version>
    </dependency>
</dependencies>

这是我的资源

@Path("student")
public class StudentResource {

private static Map<Integer, Students> stud_data = new HashMap<Integer, Students>();

static{
    for(int i = 0; i < 10; i++){

        Students stu = new Students();
        int id = i+1;
        stu.setId(id);
        stu.setName("My Student "+id);
        stu.setAge(new Random().nextInt(15)+1);

        stud_data.put(id, stu);
    }
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Students> getall(){
    return new ArrayList<Students>(stud_data.values());
}
}

请帮帮我,我怎样才能显示像json这样的xml数据。

提前致谢

【问题讨论】:

  • @Produces(MediaType.APPLICATION_XML)?您可以拥有多种媒体类型,只需将它们添加在{.., ..} 之间,用逗号分隔即可。您还需要确保将请求上的 Accept 标头设置为您想要的类型。
  • 我只给@Produces(MediaType.APPLICATION_XML),但显示错误消息HTTP Status 500 - Internal Server Error
  • 添加these ExceptionMapper 之一。但是使用泛型类型Exception,即ExceptionMapper&lt;Exception&gt;。我的猜测是这是一个抛出的 JAXB 异常,它没有被异常映射器处理。只需在映射器中打印堆栈跟踪。如果您看到堆栈跟踪,请发布它。
  • 还要确保 Student 类使用 @XmlRootElement 注释

标签: java json xml web-services jersey


【解决方案1】:

根据Jersey documentation,您可以使用 Moxy 或 JAXB RI 将对象转换为 xml 文档。如果你想使用 Moxy,你需要做一些进一步的配置,defined here。但是如果你想使用参考实现添加 jaxb 依赖。

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>2.16</version>
</dependency>

首先,您必须使用正确的 jaxb 注释来注释您的类。这是一个示例(不是最小的简单@XmlRootElement 也可以)。

@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {
        "id",
        "name",
        "email"
})
public class StudentDto {

    @XmlElement(name = "id", nillable = false, required = false)
    private Long id;

    @XmlElement(name = "name", nillable = true, required = true)
    private String name;

    @XmlElement(name = "email", nillable = false, required = false)
    private String email;

    // GETTERS && SETTERS
}

然后,您必须对您的服务进行注释,使其既可以使用又可以生成 XML 文档。例如

@Path("student")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class StudentResource {

}

最后,您的客户端必须使用正确的标头,告诉服务器它正在执行什么样的请求以及它正在等待什么样的响应。这是这些标题

Content-Type: application/xml
Accept: application/xml

这意味着为了运行 getAllStudents 服务,您需要执行类似的操作(更新:不需要 Content-Type,因为 get 中没有内容。感谢@peeskillet 的评论)。

curl -i \
    -H "Accept: application/xml" \
    -X GET \
    http://your_endpoint:8080/your_context/your_root_path/student

【讨论】:

  • Content-Type 不是 GET 所必需的(除非您实际发送数据)。没有内容被发送以具有 :-) 的类型
  • 太棒了,添加对 jersey-media-jaxb 的依赖解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多