我正在尝试从 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;
}
}
根据文档,我应该得到所需的输出,除非我遗漏了一些东西
jackson-module-jaxb-annotations
模块,然后使用 ObjectMapper
进行注册。
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());
@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。
要使用此功能读取
@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;
}
}
如果我们在 Jaxbclasses 中从 Javax 迁移到 Jakarta,那么下面的内容应该会有所帮助
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.registerModule(new JakartaXmlBindAnnotationModule());