Tom
Tom

Reputation: 44821

Best way to configure the output XML version for jaxb via resteasy on jboss

I have data that I'm serializing that has characters that aren't allowed in xml version 1.0:

<value>this &#18; is not good for 1.0</value>

When RESTEasy serializes this via JAXB it produces this:

<?xml version="1.0" encoding="UTF-8"?>
<value>this &#18; is not good for 1.0</value>

Which XML parsers will not parse as 1.0 does not allow that character, if I set the xml version to 1.1 parsers are happy.

I can do this via:

transformer.setOutputProperty(OutputKeys.VERSION, "1.1");

So what I'm wanting to know is what's the best way to configure jboss / resteasy / jaxb such that when it creates the transformer it uses that it's configured with this output property.

Upvotes: 3

Views: 2469

Answers (1)

bdoughan
bdoughan

Reputation: 148977

You can set the following on the Marshaller to create a new header.

    // Remove the header that JAXB will generate
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    // Grab the encoding that will be used for Marshalling
    String encoding = (String) marshaller.getProperty(Marshaller.JAXB_ENCODING);

    // Specify the new header
    marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.1\" encoding=\"" + encoding + "\">");

In a JAX-RS environment you can create a MessageBodyWriter to configure a Marshaller this way. The following answer to a similar question includes an example of how to do this:

Upvotes: 3

Related Questions