我试图从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;
}
}
根据文档,我应该得到所需的输出,除非我错过了什么
4条答案
按热度按时间hts6caw31#
默认情况下不支持JAXB注解。您需要添加
jackson-module-jaxb-annotations
模块,然后将其注册到ObjectMapper
。0h4hbjxa2#
如注解中所述,将
@JsonProperty
添加到字段中。我用的是Jackson2.7.0:
XML和JSON输出都使用PascalCase。
5n0oy7gb3#
要使用此功能读取
spring-boot
及其Jackson2ObjectMapperBuilder
中的@JsonProperty
和XmlElement
,您可以轻松地创建一个Bean,以便在每次需要时获取Builder。现在,您可以自动连接构建器,并在需要的地方直接配置ObjectMapper:
de90aj5v4#
如果我们在Jaxbclasses中从Javax迁移到Jakarta,那么下面应该会有所帮助