Jackson not capitalizing XML elements

D.Tomov :

Let's say I have following XML model:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "person",
        "user"
})
@XmlRootElement(name = "CUSTOMER")
public class CUSTOMER {
    @XmlElement(name = "PERSON", required = true)
    protected CUSTOMER.PERSON person;
    @XmlElement(name = "USER", required = true)
    protected CUSTOMER.USER user;
 ....
 ....

If I rename class following conventions as:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "person",
        "user"
})
@XmlRootElement(name = "CUSTOMER")
public class Customer {
....

And both elements like:

    @XmlElement(name = "PERSON", required = true)
    protected CUSTOMER.PERSON person;
    @XmlElement(name = "USER", required = true)
    protected CUSTOMER.USER user;
    ....

The XML from

new XmlMapper().writerWithDefaultPrettyPrinter().writeValueAsString(customer)) 

looks like this:

<Customer>
  <id>id</id>
  ...
  <person>
    <id>id</id>
    ...
  </person>
  <user>
    <id>id</id>
    ...
  </user>
</Customer>

But I want it to look like this:

<CUSTOMER>
  <id>id</id>
  ...
  <PERSON>
    <id>id</id>
    ...
  </PERSON>
  <USER>
    <id>id</id>
    ...
  </USER>
</CUSTOMER>

Am I doing something wrong? I saw that I can use @JsonProperty and it works, but only for the fields and I feel wrong using JsonProperty on an XML element. Is it okay for my model classes to be named CUSTOMER, PERSON, USER or should I rename them, and if its better to rename them should I use JsonProperty? What about the RootElement?

cassiomolin :

Looks like Jackson is not parsing the JAXB annotations. To fix it, you could use an annotation introspector such as JaxbAnnotationIntrospector:

XmlMapper mapper = new XmlMapper();
mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory()));

String xml = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(costumer);

Also check the JaxbAnnotationIntrospector documentation, as there are some limitations.


If, for some reason, you intend to use JAXB directly, as you mentioned in the comments, you would have something like:

JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

StringWriter writer = new StringWriter();
marshaller.marshal(customer, writer);

String xml = writer.toString();

Just bear in mind the JAXBContext should be created once and then reused, as pointed out in this answer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165966&siteId=1