使用Jersey Framework使用CDATA对对象进行编组 [英] Marshalling of Object with CDATA using Jersey Framework
问题描述
我想使用CDATA块封送我的对象。我可以通过为CharacterEscapeHandler创建marshaller和设置属性来做到这一点(http://stackoverflow.com/questions/14193944/jaxb-marshalling-unmarshalling-with-cdata)。但在泽西岛,编组是由球衣完成的。那么我怎么能告诉球衣用CDATA编组对象。
i want to marshal my object using CDATA block. i can do this with creating marshaller and setting property for CharacterEscapeHandler(http://stackoverflow.com/questions/14193944/jaxb-marshalling-unmarshalling-with-cdata). but in Jersey marshalling is done by jersey. so how can i tell jersey to marshal object with CDATA.
我有以下服务
@GET
@Path("/getdata")
@Produces(MediaType.TEXT_XML)
public HelloBean getData() throws Exception
{
HelloBean h1 = new HelloBean();
h1.setName("kshitij");
return h1;
}
和bean类是
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class HelloBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我尝试过添加Adapter类。但问题是如何将其他属性设置为泽西使用的默认marshaller。
i have tried with adding Adaptor class. but problem is how can i set additional property to default marshaller which jersey is using.
我想设置以下属性。
marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
public void escape(char[] ac, int i, int j, boolean flag,
Writer writer) throws IOException {
writer.write( ac, i, j ); }
});
推荐答案
你可以创建一个JAX-RS MessageBodyWriter
。 MessageBodyWriter
允许您使用自己的代码编写XML消息。
You can create a JAX-RS MessageBodyWriter
. A MessageBodyWriter
allows you use your own code to write the XML message.
相关示例
- JAXB or JAX-RS is wrapping numbers in my JSON responses with quotes, turning them into strings. Why this is the default behavior, and how to fix it?
这篇关于使用Jersey Framework使用CDATA对对象进行编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!