Esteban Rincon
Esteban Rincon

Reputation: 2110

JAX-RS change @XmlRootElement.name on runtime

I have a DTO which cannot be modified, but it is required for use on a new endpoint. The problem is that the RootElement Name has to be changed, is there a way to to this on a specific endpoint ?

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "wccevent")
public class SimpleWCCEvent {
..
}

On my new endpoint I need to change the root name from wccevent to apiresponse, is this possible ?

Something like

@GET
...
@XmlFeatures({overrideRootName = "apiresponse"})
public Response newEndpoint(){
...
}

Upvotes: 0

Views: 271

Answers (1)

Ntobeko Mkhize
Ntobeko Mkhize

Reputation: 112

Is the need for the change int the DTO due to a broader change in the API? Maybe you might need to consider how you handle API changes in the greater schema of things.

Else have a look at this answer that suggests overriding the root element name as such:

String xml mapper.writer().withRootName("movies").writeValueAsString(movies);

Upvotes: 1

Related Questions