2
votes

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

1 Answers

0
votes

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

Related Example