Marshalling of Object with CDATA using Jersey Framework

落爺英雄遲暮 提交于 2019-12-11 04:29:30

问题


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.

i have following service

@GET
    @Path("/getdata")
    @Produces(MediaType.TEXT_XML)
    public HelloBean getData() throws Exception 
    {
        HelloBean h1 = new HelloBean();
        h1.setName("kshitij");
        return h1;
    }

and bean Class is

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;
    }
}

i have tried with adding Adaptor class. but problem is how can i set additional property to default marshaller which jersey is using.

i want to set following property.

 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 ); }
                });

回答1:


You can create a JAX-RS MessageBodyWriter. A MessageBodyWriter allows you use your own code to write the XML message.

Related Example

  • 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?


来源:https://stackoverflow.com/questions/14229877/marshalling-of-object-with-cdata-using-jersey-framework

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!