I would like my controller to return the following XML:
<?xml version="1.0"?>
<ResponseDTO result=”OK” timestamp=”20110322T152403Z">
<myId>9999.99</myId>
<currency>USD</currency>
</ResponseDTO >
Basically my DTO looks like this:
public class ResponseDTO {
private String myId;
private String currency;
}
And my controller looks like this:
@RequestMapping(produces = MediaType.APPLICATION_XML_VALUE)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public ResponseDTO doSomething() {
...
return new ResponseDTO();
}
I'm using the following dependency, gradle snippet given below:
compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version:'2.8.2'
How is it possible to achieve the 'result=OK' and 'timestamp=20110322T152403Z' properties with jackson xml parser?
Use JacksonXmlProperty to set attributes to the jackson root element like below:
@JacksonXmlRootElement
public class ResponseDTO {
@JacksonXmlProperty(isAttribute=true)
private String response;
@JacksonXmlProperty(localName="myId")
private String myId;
@JacksonXmlProperty(localName="currency")
private String currency;
}
Result:
<ResponseDTO response="test">
<myId>test</myId>
<currency>test</currency>
</ResponseDTO>