【问题标题】:XMl formation in jersey with @rootelement带有@rootelement 的球衣中的 XMl 阵型
【发布时间】:2013-10-01 11:15:49
【问题描述】:

我正在使用 jersey REST JAX-RS 来开发 REST Web 服务。我想以以下格式返回 xml-

<RootChild>
<Child1>
<HFact>
<a></a>
<b></b>
<c></c>
<d></d>
</HFact>

<PFact>
<a></a>
<b></b>
<c></c>
<d></d>
</PFact>
</Child1>
....
</RootChild>

我在每个 POJO 类中都使用@RootElement。但 xml 树不会到来。

【问题讨论】:

  • 您的 JAX-RS 方法、对象模型和当前 XML 结果是什么样的?
  • @BlaiseDoughan 抱歉,我在正确配置 Pojo 类时犯了一些错误。因此,我没有得到 xml/json。现在我自己解决这个问题。非常感谢您的关心。

标签: java xml rest jaxb jersey


【解决方案1】:

要让 Jersey RESTful Web 服务生成 XML 中的调用响应,您需要声明调用(实现调用的方法)@Produces({"application/xml"}),这应该足够了,前提是您正确配置了 JAXB( POJO,您将其称为 API 调用的响应,并在调用中构建响应。

泽西岛声明示例:

@Path("/resource")
@Produces({ "application/xml"})
public class ResourceAPI{

    @GET
    @Path("/childs")
    @Produces("application/xml")
    public GetChildsResp
            getChilds(){

        GetChildsResp response = new GetChildsResp();

        // build and populate response with all the Childs (from your DB)

        return response;

    }

}

【讨论】:

  • 我想要这种格式的 xml,我将 @RootElement 放在每个 POJO 类中。位没有显示。
  • 我已经扩展了我的回复...添加 POJO 声明以便我们可以进一步帮助您。
猜你喜欢
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
相关资源
最近更新 更多