Search code examples
javaspring-bootjacksonjackson-dataformat-xml

Remove < in Response body of xml


My response always shows &lt; for one of the values inside XML instead of <.

Below is my XML MyResponseEnity:

@JacksonXmlRootElement(localName = "test")
public class MyResponseEnity implements Serializable {

    @JacksonXmlProperty
    private Result result;
    @JacksonXmlProperty
    private String name;
    @JacksonXmlProperty
    private String age;

}

public class Result {
    @JacksonXmlProperty(isAttribute = true)
    private final String val = MediaType.APPLICATION_XML();
    @JacksonXmlText
    private String value;
}

The reason for not converting &lt; to < is because of the value for:

@JacksonXmlText
private String value;

is an actual xml but in the form of a string, something like <history>abc</history>.

I tried adding httpmesssgaeconverters in configuration(i am using spring boot)

 @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
        builder.indentOutput(true);
        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
    }

Controller:

return new ResponseEntity<MyResponseEnity>(myResponseEnity,
        HttpStatus.OK);

Can anyone tell what I am doing wrong and how can I get &lt; converted to <

This is my current result:

<test>
<result val="application/xml">&lt;history>abc&lt;/history></result>
<name>myname</name>
<age>myage</age>
</test>

Solution

  • You can add @JsonRawValue annotation to the field, which will be considered as a raw XML value when serializing.