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

rbpvctlc  于 2023-09-29  发布在  Java
关注(0)|答案(4)|浏览(179)

我试图从XSD->POJO->JSON与UPS跟踪API一起使用,这是区分大小写的。我在生成的JSON中使用Jackson2.6.7。我看到 Camel 的情况下,我应该看到下面的名称:
"TrackRequest": { "InquiryNumber": "1Z12345E6205277936" }
生成的Java bean注解如下:

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

我已经尝试了一些Map功能设置,如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;
    }

}

根据文档,我应该得到所需的输出,除非我错过了什么

hts6caw3

hts6caw31#

默认情况下不支持JAXB注解。您需要添加jackson-module-jaxb-annotations模块,然后将其注册到ObjectMapper

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

0h4hbjxa2#

如注解中所述,将@JsonProperty添加到字段中。
我用的是Jackson2.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

5n0oy7gb

5n0oy7gb3#

要使用此功能读取spring-boot及其Jackson2ObjectMapperBuilder中的@JsonPropertyXmlElement,您可以轻松地创建一个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;
    }
}
de90aj5v

de90aj5v4#

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

XmlMapper xmlMapper = new XmlMapper();
      xmlMapper.registerModule(new JakartaXmlBindAnnotationModule());

相关问题