JAXB java对象转换为xml,创建Pascal Case xml元素
通过jaxb插件从wsdl生成对象JAXB java对象转换为xml,创建Pascal Case xml元素,java,spring,maven,jaxb,maven-jaxb2-plugin,Java,Spring,Maven,Jaxb,Maven Jaxb2 Plugin,通过jaxb插件从wsdl生成对象 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "WebAuthenticationCredential", propOrder = { "key", "password" }) public class WebAuthenticationCredential { @XmlElement(name = "Key", required = true) protected
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "WebAuthenticationCredential", propOrder = {
"key",
"password"
})
public class WebAuthenticationCredential {
@XmlElement(name = "Key", required = true)
protected String key;
@XmlElement(name = "Password", required = true)
protected String password;
方法:
public RateReply getRates(
Request sRequest) {
try {
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<WebAuthenticationCredential> soapRateRequest =
objectFactory.createRateRequest(sRequest);
XmlMapper xmlMapper = new XmlMapper();
String xmlContent = xmlMapper.writeValueAsString(soapRateRequest);
logger.info(xmlContent); // check below the response
Reply rReply = (Reply) JAXBIntrospector
.getValue(WebServiceTemplate.marshalSendAndReceive(serviceUri, soapRateRequest));
handleErrors(rateReply);
return rateReply;
} catch (SoapFaultClientException e) {
logger.error("Caught exception rates", e);
throw new Exception("Failed calling service ", null);
}
}
<JAXBElement>
<name>PreRequest</name>
<declaredType>Request</declaredType>
<scope>javax.xml.bind.JAXBElement$GlobalScope</scope>
<value>
<webAuthenticationDetail>
<key>test</key>
<password>password</password>
</webAuthenticationDetail>
</value>
</JAXBElement>
public rate回复getRates(
请求(请求){
试一试{
ObjectFactory ObjectFactory=新的ObjectFactory();
JAXBElement soapRateRequest=
objectFactory.createRateRequest(sreRequest);
XmlMapper XmlMapper=新的XmlMapper();
字符串xmlContent=xmlMapper.writeValueAsString(soapRateRequest);
logger.info(xmlContent);//检查下面的响应
Reply rReply=(Reply)JAXBIntrospector
.getValue(WebServiceTemplate.marshalSendAndReceive(serviceUri,soapRateRequest));
处理错误(费率回复);
回复率;
}捕获(SoapFaultClientException e){
错误(“捕获的异常率”,e);
抛出新异常(“调用服务失败”,null);
}
}
JAXB生成的ObjectFactory类的createRequest方法:
/**
* Create an instance of {@link JAXBElement }{@code <}{@link RateRequest }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://fedex.com/ws/rate/v14", name = "PreAuthorizedRateRequest")
public JAXBElement<RateRequest> createPreAuthorizedRateRequest(RateRequest value) {
return new JAXBElement<RateRequest>(_PreAuthorizedRateRequest_QNAME, RateRequest.class, null, value);
/**
*创建{@link JAXBElement}{@code}的实例
*
*/
@XmlElementDecl(命名空间=”http://fedex.com/ws/rate/v14,name=“PreAuthorizedRateRequest”)
公共JAXBElement createPreAuthorizedRateRequest(RateRequest值){
返回新的JAXBElement(_PreAuthorizedRateRequest_QNAME,RateRequest.class,null,value);
xmlContent的记录器信息(使用驼峰案例元素打印)为
public RateReply getRates(
Request sRequest) {
try {
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<WebAuthenticationCredential> soapRateRequest =
objectFactory.createRateRequest(sRequest);
XmlMapper xmlMapper = new XmlMapper();
String xmlContent = xmlMapper.writeValueAsString(soapRateRequest);
logger.info(xmlContent); // check below the response
Reply rReply = (Reply) JAXBIntrospector
.getValue(WebServiceTemplate.marshalSendAndReceive(serviceUri, soapRateRequest));
handleErrors(rateReply);
return rateReply;
} catch (SoapFaultClientException e) {
logger.error("Caught exception rates", e);
throw new Exception("Failed calling service ", null);
}
}
<JAXBElement>
<name>PreRequest</name>
<declaredType>Request</declaredType>
<scope>javax.xml.bind.JAXBElement$GlobalScope</scope>
<value>
<webAuthenticationDetail>
<key>test</key>
<password>password</password>
</webAuthenticationDetail>
</value>
</JAXBElement>
预先请求
要求
javax.xml.bind.JAXBElement$GlobalScope
测试
密码
预期输出:内部的所有元素都应该是pascal大小写(WebAuthenticationDetail,Key,password),是否可以使用JAXB
<JAXBElement>
<name>PreRequest</name>
<declaredType>Request</declaredType>
<scope>javax.xml.bind.JAXBElement$GlobalScope</scope>
<value>
<WebAuthenticationDetail>
<Key>test</Key>
<Password>password</Password>
</WebAuthenticationDetail>
</value>
</JAXBElement>
预先请求
要求
javax.xml.bind.JAXBElement$GlobalScope
测试
密码
默认情况下,XmlMapper无法识别JAXB注释。要启用它们,您需要注册模块JaxbAnnotationModule
例如:
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.registerModule(new JaxbAnnotationModule());
默认情况下,XmlMapper不识别JAXB注释。要启用它们,需要注册模块JaxbAnnotationModule 例如:
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.registerModule(new JaxbAnnotationModule());
我添加XmlMapper只是为了将soapRequest转换为字符串并在日志中打印出来,XmlMapper没有在实际的方法中使用。您是指其他任何东西吗?所以问题是,这些值没有与此请求一起以Pascal大小写发送:“WebServiceTemplate.MarshallSendReceive(serviceUri,soapRateRequest)”?那么您可以发布整个类的“public rate reply getRates”方法吗?我添加XmlMapper只是为了将soapRequest转换为字符串并在日志中打印出来,XmlMapper在实际的方法中没有被使用。您指的是其他方法吗?所以问题是,该请求中的值不是以Pascal格式发送的:“WebServiceTemplate.marshalSendAndReceive(serviceUri,soapRateRequest)”?那么您可以发布整个类的“public RateReply getRates”方法吗?