Jackson 在编组 JSON 时忽略 XmlElement 名称/大小写

问题描述 投票:0回答:4

我正在尝试从 XSD->POJO->JSON 与区分大小写的 UPS 跟踪 API 一起使用。我在生成的 JSON 中使用 Jackson 2.6.7。当我看到下面的内容时,我看到了驼峰命名法:

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

生成的 Java bean 注释如下:

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

我尝试了一些映射功能设置,例如 USE_WRAPPER_NAME_AS_PROPERTY_NAME 和 USE_STD_BEAN_NAMING,但似乎没有达到预期的结果。

我像这样生成 JSON:

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

upsRequest bean 如下所示:

@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;
    }

}

根据文档,我应该得到所需的输出,除非我遗漏了一些东西

java json xsd jersey jackson
4个回答
24
投票

默认情况下不支持 JAXB 注释。您需要添加

jackson-module-jaxb-annotations
模块,然后使用
ObjectMapper
进行注册。

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());

0
投票

如评论中所述,将

@JsonProperty
添加到字段中。

使用 Jackson 2.7.0 对我来说效果很好:

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

    @XmlElement(name = "TrackRequest")
    @JsonProperty("TrackRequest")
    private TrackRequest trackRequest;

    public Request() {
    }
    public Request(TrackRequest trackRequest) {
        this.trackRequest = trackRequest;
    }
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "inquiryNumber" })
class TrackRequest {

    @XmlElement(name = "InquiryNumber")
    @JsonProperty("InquiryNumber")
    private String inquiryNumber;

    public TrackRequest() {
    }
    public TrackRequest(String inquiryNumber) {
        this.inquiryNumber = inquiryNumber;
    }
}

测试

Request upsRequest = new Request(new TrackRequest("1Z12345E6205277936"));
Marshaller marshaller = JAXBContext.newInstance(Request.class).createMarshaller();
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
marshaller.marshal(upsRequest, System.out);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.writeValue(System.out, upsRequest);

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request>
    <TrackRequest>
        <InquiryNumber>1Z12345E6205277936</InquiryNumber>
    </TrackRequest>
</Request>
{
  "TrackRequest" : {
    "InquiryNumber" : "1Z12345E6205277936"
  }
}

XML 和 JSON 输出均使用 PascalCase


0
投票

要使用此功能读取

@JsonProperty
中的
XmlElement
spring-boot
及其
Jackson2ObjectMapperBuilder
,您可以轻松创建一个 Bean 以在每次需要时获取 Builder。

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    //configure features for serialization and deserialization
    return builder;
}

您现在可以自动装配构建器并直接在需要的地方配置 ObjectMapper:

@Component
public class MyClass {

    private ObjectMapper objectMapper;

    public MyClass(Jackson2ObjectMapperBuilder jacksonBuilder) {
        super();
        objectMapper = jacksonBuilder.build().registerModule(new JaxbAnnotationModule());
    }

    public String serialize(){
        AnyObject ao = new AnyObject();
        String json = objectMapper.writeValueAsString(ao);
        return json;
    }
}

0
投票

如果我们在 Jaxbclasses 中从 Javax 迁移到 Jakarta,那么下面的内容应该会有所帮助

 XmlMapper xmlMapper = new XmlMapper();
      xmlMapper.registerModule(new JakartaXmlBindAnnotationModule());
© www.soinside.com 2019 - 2024. All rights reserved.