Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson ignoring XmlElement name/case when marshalling JSON

I am trying to go from XSD->POJO->JSON for use with UPS tracking API which is case sensitive. I'm using Jackson 2.6.7 In the generated JSON. I am seeing camel case names when I should see below:

"TrackRequest": { "InquiryNumber": "1Z12345E6205277936" }

The generated Java bean is annotated like so:

@XmlElement(name = "TrackRequest")
protected TrackRequest trackRequest;

I've tried a few mapping feature settings such as USE_WRAPPER_NAME_AS_PROPERTY_NAME and USE_STD_BEAN_NAMING which don't appear to have the desired result.

I'm generating the JSON like so:

ObjectMapper mapper = new ObjectMapper();
String jsonRequest = mapper.writeValueAsString(upsRequest);

The upsRequest bean looks like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "upsSecurity",
    "trackRequest"
})
@XmlRootElement(name = "Request")
public class Request {

    @XmlElement(name = "UPSSecurity")
    protected UPSSecurity upsSecurity;
    @XmlElement(name = "TrackRequest")
    protected TrackRequest trackRequest;

    /**
     * Gets the value of the upsSecurity property.
     * 
     * @return
     *     possible object is
     *     {@link UPSSecurity }
     *     
     */
    public UPSSecurity getUPSSecurity() {
        return upsSecurity;
    }

    /**
     * Sets the value of the upsSecurity property.
     * 
     * @param value
     *     allowed object is
     *     {@link UPSSecurity }
     *     
     */
    public void setUPSSecurity(UPSSecurity value) {
        this.upsSecurity = value;
    }

    /**
     * Gets the value of the trackRequest property.
     * 
     * @return
     *     possible object is
     *     {@link TrackRequest }
     *     
     */
    public TrackRequest getTrackRequest() {
        return trackRequest;
    }

    /**
     * Sets the value of the trackRequest property.
     * 
     * @param value
     *     allowed object is
     *     {@link TrackRequest }
     *     
     */
    public void setTrackRequest(TrackRequest value) {
        this.trackRequest = value;
    }

}

According to the docs, I should be getting the desired output unless I'm missing something

like image 293
Half_Duplex Avatar asked Dec 03 '22 22:12

Half_Duplex


1 Answers

JAXB annotations are not by default supported. You need to add the jackson-module-jaxb-annotations module and then register that with the ObjectMapper.

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());
like image 144
Paul Samsotha Avatar answered Dec 06 '22 13:12

Paul Samsotha