I have a field that I want formatted differently when using XML and JSON, though Jackson only reads the JAXB annotation @XMLElement
but ignores the Jackson annotation @JsonProperty
when serializing to JSON.
@XmlElementWrapper(name = "ParticipantBindings")
@XmlElement(name = "ParticipantBinding")
@com.fasterxml.jackson.annotation.JsonProperty("participantBindings") // This is ignored?
public List<ParticipantBinding> getParticipantBindings() {
return participantBindings;
}
My Spring boot controller is called as so
@GetMapping(path = "/{id}", produces = {MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<Participant> getParticipantById(@PathVariable("id") int id) {
Participant p = participantDao.getParticipantFull(id);
if (p == null) {
throw new ResourceNotFoundException();
} else {
return ResponseEntity.ok(p);
}
}
Where the expected result for sending a getrequest with accept-header: application/xml would be:
<ParticipantBindings>
<ParticipanBinding>...</ParticipantBinding>
<ParticipanBinding>...</ParticipantBinding>
<ParticipantBindings>
Where for application/json the expected result should be:
"participantBindings": [ ...]
And although XML is correctly formatted, the JSON is outputted as follows:
"ParticipantBinding" : [ ...]