简体   繁体   English

在REST服务中返回XML响应

[英]Return XML Response in REST Service

I am writing a RESTful web service where in I want to return a XML containing some resultset. 我正在编写一个RESTful Web服务,我希望返回一个包含一些结果集的XML。 I have used XSTREAM and parsed the object into XML string. 我使用了XSTREAM并将对象解析为XML字符串。 Since I need to return this string, I need to know how to pass it back to the calling client. 由于我需要返回此字符串,因此我需要知道如何将其传递回调用客户端。

One way is to return the RESPONSE to the calling client. 一种方法是将RESPONSE返回给调用客户端。 And my sample code here shows what it is that I am trying to do. 我的示例代码显示了我正在尝试做的事情。

@Path("somepath")
public class ClassToReturnXML
{
    public Response methodToReturnXML()
    {

       ResponseBuilder builder = new ResponseBuilderImpl();
       builder.type(MediaType.TEXT_XML);
       builder.entity(myXMLString);
       return builder.build();
    }
}

Unfortunately it doesn't return the entity, though the status code is 200. Am I instantiating the ResponseBuilder incorrectly? 不幸的是它没有返回实体,虽然状态代码是200.我是否错误地实例化了ResponseBuilder? I also saw somewhere that it should be instantiated as follows: 我还看到了应该如下实例化的地方:

ResponseBuilder builder = Response.status(200);

Please suggest what is the apt way to return XML in response. 请建议在响应中返回XML的适当方法是什么。

I AM USING APACHE CXF for RESTFUL SERVICES. 我正在使用APACHE CXF进行RESTFUL服务。 (Version 2.2.3 -- i guess) :D Thanks in Advance for all the help. (版本2.2.3 - 我猜):D在预先感谢所有帮助。

It was just a cleaning problem. 这只是一个清洁问题。 It eventually worked. 它最终奏效了。 I created the response in following way eventually. 我最终以下列方式创建了响应。

Response response = Response.status(200).type(MediaType.TEXT_XML).entity(xmlString).build();

It works just fine. 它工作得很好。 I hope it helps someone. 我希望它对某人有帮助。

Does the HTTP Response have the correct content-type header to identify that it is Xml ie text/xml or application\\xml ? HTTP响应是否具有正确的内容类型标头以标识它是Xml,即text/xml还是application\\xml Checkout The Proper Content Type for XML Feeds . 检查XML Feed的正确内容类型

The response status 200 is just one of the standard HTTP Response Codes that means the request has succeeded so only return it if that is the case. 响应状态200只是标准HTTP响应代码之一 ,这意味着请求已成功,因此只有在这种情况下才返回它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM