如何解决使用jersey在java rest服务中生成xml的问题?

时间:2015-09-03 07:41:45

标签: java json xml web-services 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数据。

提前致谢

1 个答案:

答案 0 :(得分:3)

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