如何将JAXBElement编组为Response? [英] How to marshal JAXBElement into Response?
问题描述
我的 CXF
提供 REST
服务通常返回 javax.ws.rs.core。响应
通常的原因是,将结果实体数据封装为XML并返回代码:
My CXF
provided REST
services usually return javax.ws.rs.core.Response
for the usual reason, to encapsulate the result entity data marshaled as XML and a return code:
@GET
@Path("/getPojo")
@Produces("application/xml")
public Response getPojo() {
SomePojo resultObj = ...;
Response result = Response.status(200).entity(resultObj).build();
return result;
}
需要 SomePojo
包含正确的注释:
@XmlRootElement(name = "somePojo")
@XmlAccessorType(XmlAccessType.FIELD)
public class SomePojo implements Serializable {
...
}
但是,现在我面临的情况是注释约定对我不起作用,我必须构建自己的 JAXBElement
。如何在 Response
中包含自定义封送的JAXBElement,而不是使用 Response.ResponseBuilder.entity(resultObj)
,哪个依赖于注释配置?我正在整理类似此处的内容。但他只是将封送的XML打印到控制台中,我想将它打印到Response中(而不仅仅是 HttpResponse
out)。
However, now I am facing a scenario where the annotation convention does not work for me and I have to build my own JAXBElement
. How can I include the custom-marshaled JAXBElement in the Response
instead of using Response.ResponseBuilder.entity(resultObj)
, which relies on the annotation configuration? I am marshaling something similar to what is explained here but he's just printing the marshaled XML into the console and I would like to print it into the Response (and not just HttpResponse
out).
推荐答案
您可以使用自定义编组器对xml进行编组,并在响应
的实体中设置结果XML, as String
或 InputStream
You can marshall the xml using your custom marshaller and set the resultant XML in the entity of the Response
, as String
or InputStream
@GET
@Path("/getXML")
@Produces("application/xml")
public Response getXML() {
String xml = // custom marshall
Response result = Response.
status(200).
entity(xml).
type("application/xml").
build();
return result;
}
这篇关于如何将JAXBElement编组为Response?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!