泽西岛消费XML帖子 [英] Jersey Consumes XML post

查看:91
本文介绍了泽西岛消费XML帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发布到"Jersey Rest"服务.这样做的标准方法是什么?

I want to make a Post to Jersey Rest service. What is the standard way of doing this?

@Post
@Consumes(MediaType.Application_xml)
public Response method(??){}

推荐答案

假设您有一个Java Bean,例如雇员Bean.添加标签以告知

Suppose you have a java bean say an employee bean such as. Add the tags to tell

@XmlRootElement (name = "Employee")
public class Employee {
    String employeeName;

    @XmlElement
    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }
}

@XmlRootElement告知这将是xml中的主要标记.在这种情况下,您也可以为主要标签指定一个名称.

@XmlRootElement tells that this will be the main tag in xml. In this case you can specify a name for the main tag as well.

@XmlElement告知这将是根标签内的子标签

@XmlElement tells that this would be the sub tag inside the root tag

说,将在发布请求中作为正文一部分的示例xml看起来像

Say, the sample xml that will come as a part of body in the post request will look something like

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
 <employeeName>Jack</employeeName>
</Employee>

在编写Web服务以接受此类xml时,我们可以编写以下方法.

When writing a webservice to acccept such an xml we can write the following method.

@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response getEmployee(Employee employee) {
     employee.setEmployeeName(employee.getEmployeeName() + " Welcome");
     return Response.status(Status.OK).entity(employee).build();
}

调用此服务时,您将获得以下xml作为响应的一部分.

On calling this service, you will get the following xml as part of the response.

<Employee>
<employeeName> Jack Welcome </employeeName>
</Employee>

使用@Xml ...注释,解组和封送请求和响应对象变得非常容易.

using @Xml...annotations, it becomes very easy to unmarshal and marshal the request and response objects.

只需使用MediaType.APPLICATION_JSON而不是APPLICATION_XML,就可以对JSON输入和JSON输出采用类似的方法

Similar approach can be taken for JSON input as well as JSON output by just using the MediaType.APPLICATION_JSON instead of APPLICATION_XML

因此,对于xml作为输入,您可以将xml作为输出作为http响应的一部分. 希望这会有所帮助.

So for an xml as input, you can get an xml as an output as part of the http response. Hope this helps.

这篇关于泽西岛消费XML帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆