JAXB:从 WebServiceTemplate 返回的子元素为空
JAXB: Child Element Returning from WebServiceTemplate is null
我正在使用 string-ws 创建网络服务客户端并从网络服务中获取数据。我使用 org.jvnet.jaxb2.maven2:maven-jaxb2-plugin 来生成 java classes。下面给出了一些由这个插件生成的 classes:
GetLandingPage常见问题解答:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getLandingPageFAQ", propOrder = {
"faq"
})
public class GetLandingPageFAQ {
protected FaqListInBean faq;
public FaqListInBean getFaq() {
return faq;
}
public void setFaq(FaqListInBean value) {
this.faq = value;
}
}
FaqListInBean:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "faqListInBean", propOrder = {
"viewList",
"localList",
"numberOfContent"
})
public class FaqListInBean {
@XmlElement(nillable = true)
protected List<String> viewList;
@XmlElement(nillable = true)
protected List<String> localList;
protected int numberOfContent;
public List<String> getViewList() {
if (viewList == null) {
viewList = new ArrayList<String>();
}
return this.viewList;
}
public List<String> getLocalList() {
if (localList == null) {
localList = new ArrayList<String>();
}
return this.localList;
}
public int getNumberOfContent() {
return numberOfContent;
}
public void setNumberOfContent(int value) {
this.numberOfContent = value;
}
}
GetLandingPageFAQ 响应:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getLandingPageFAQResponse", propOrder = {
"faqListOutBean"
})
public class GetLandingPageFAQResponse {
@XmlElement(name = "FaqListOutBean", namespace = "omitted")
protected FaqListOutBean faqListOutBean;
public FaqListOutBean getFaqListOutBean() {
return faqListOutBean;
}
public void setFaqListOutBean(FaqListOutBean value) {
this.faqListOutBean = value;
}
}
WSDL 是:
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="omitted" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="omitted" name="MyService">
<types>
<xsd:schema>
<xsd:import namespace="omitted" schemaLocation="http://url?xsd=1"/>
</xsd:schema>
</types>
<message name="getLandingPageFAQ">
<part name="parameters" element="tns:getLandingPageFAQ"/>
</message>
<message name="getLandingPageFAQResponse">
<part name="parameters" element="tns:getLandingPageFAQResponse"/>
</message>
<message name="WsException">
<part name="fault" element="tns:WsException"/>
</message>
</message>
<portType name="MyServiceImpl">
<operation name="getLandingPageFAQ">
<input message="tns:getLandingPageFAQ"/>
<output message="tns:getLandingPageFAQResponse"/>
<fault message="tns:WsException" name="WsException"/>
</operation>
</portType>
<binding name="MyServiceImplPortBinding" type="tns:MyServiceImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getLandingPageFAQ">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="WsException">
<soap:fault name="WsException" use="literal"/>
</fault>
</operation>
</binding>
<service name="MyService">
<port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
<soap:address location="http://url/MyService"/>
</port>
</service>
</definitions>
xsd是:
<xs:schema xmlns:tns="omitted" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="omitted">
<xs:element name="WsException" type="tns:WsException"/>
<xs:element name="FaqListInBean" type="tns:faqListInBean"/>
<xs:element name="FaqListOutBean" type="tns:faqListOutBean"/>
<xs:element name="getLandingPageFAQ" type="tns:getLandingPageFAQ"/>
<xs:element name="getLandingPageFAQResponse" type="tns:getLandingPageFAQResponse"/>
<xs:complexType name="faqListInBean">
<xs:sequence>
<xs:element name="viewList" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="localList" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="numberOfContent" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="faqListOutBean">
<xs:sequence>
<xs:element name="faqList" type="tns:simpleFAQOutBean" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="categoryList" type="tns:categoryOutBean" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="numberOfContent" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getLandingPageFAQ">
<xs:sequence>
<xs:element name="faq" type="tns:faqListInBean" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getLandingPageFAQResponse">
<xs:sequence>
<xs:element ref="tns:FaqListOutBean" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="WsException">
<xs:sequence>
<xs:element name="message" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
我通过直接点击 URL 来生成 Java classes,但我没有将 xsd 或 jaxb 绑定文件添加到我的项目中.这是我生成它的方式:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<id>faq--api-service</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generateDirectory>src/main/java</generateDirectory>
<generatePackage>package</generatePackage>
<removeOldOutput>true</removeOldOutput>
<cleanPackageDirectories>true</cleanPackageDirectories>
<schemas>
<schema>
<url>url</url>
</schema>
</schemas>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>1.0.1</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
ObjectFactory class 是:
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _WsException_QNAME = new QName("omitted", "WsException");
private final static QName _FaqListInBean_QNAME = new QName("omitted", "FaqListInBean");
private final static QName _FaqListOutBean_QNAME = new QName("omitted", "FaqListOutBean");
private final static QName _GetLandingPageFAQ_QNAME = new QName("omitted", "getLandingPageFAQ");
private final static QName _GetLandingPageFAQResponse_QNAME = new QName("omitted", "getLandingPageFAQResponse");
public ObjectFactory() {
}
public WsException createWsException() {
return new WsException();
}
public FaqListInBean createFaqListInBean() {
return new FaqListInBean();
}
public FaqListOutBean createFaqListOutBean() {
return new FaqListOutBean();
}
public GetLandingPageFAQ createGetLandingPageFAQ() {
return new GetLandingPageFAQ();
}
public GetLandingPageFAQResponse createGetLandingPageFAQResponse() {
return new GetLandingPageFAQResponse();
}
@XmlElementDecl(namespace = "omitted", name = "WsException")
public JAXBElement<WsException> createWsException(WsException value) {
return new JAXBElement<WsException>(_WsException_QNAME, WsException.class, null, value);
}
@XmlElementDecl(namespace = "omitted", name = "FaqListInBean")
public JAXBElement<FaqListInBean> createFaqListInBean(FaqListInBean value) {
return new JAXBElement<FaqListInBean>(_FaqListInBean_QNAME, FaqListInBean.class, null, value);
}
@XmlElementDecl(namespace = "omitted", name = "FaqListOutBean")
public JAXBElement<FaqListOutBean> createFaqListOutBean(FaqListOutBean value) {
return new JAXBElement<FaqListOutBean>(_FaqListOutBean_QNAME, FaqListOutBean.class, null, value);
}
@XmlElementDecl(namespace = "omitted", name = "getLandingPageFAQ")
public JAXBElement<GetLandingPageFAQ> createGetLandingPageFAQ(GetLandingPageFAQ value) {
return new JAXBElement<GetLandingPageFAQ>(_GetLandingPageFAQ_QNAME, GetLandingPageFAQ.class, null, value);
}
@XmlElementDecl(namespace = "omitted", name = "getLandingPageFAQResponse")
public JAXBElement<GetLandingPageFAQResponse> createGetLandingPageFAQResponse(GetLandingPageFAQResponse value) {
return new JAXBElement<GetLandingPageFAQResponse>(_GetLandingPageFAQResponse_QNAME, GetLandingPageFAQResponse.class, null, value);
}
}
现在我写的客户端是:
public class FaqApiServiceClient extends WebServiceGatewaySupport {
public FaqApiServiceClient() {
try {
String uri = getFaqUri();
setDefaultUri(uri);
} catch (IOException ex) {
throw new ClientInstantiationException(ex);
}
}
public GetLandingPageFAQResponse getLandingPageFAQResponse() {
ObjectFactory objectFactory = new ObjectFactory();
GetLandingPageFAQ faq = new GetLandingPageFAQ();
FaqListInBean bean = objectFactory.createFaqListInBean();
bean.getLocalList().add("en_US");
faq.setFaq(bean);
JAXBElement<GetLandingPageFAQ> request = new ObjectFactory().createGetLandingPageFAQ(faq);
@SuppressWarnings("unchecked")
JAXBElement<GetLandingPageFAQResponse> response = (JAXBElement<GetLandingPageFAQResponse>) getWebServiceTemplate().marshalSendAndReceive(request);
return response.getValue();
}
private String getUri() throws IOException {
InputStream stream = getClass().getClassLoader().getResourceAsStream("wsdl-url.properties");
Properties properties = new Properties();
properties.load(stream);
return properties.getProperty("FaqAPIService.WSDL");
}
}
现在返回的对象 GetLandingPageFAQResponse
不为空,但我找不到子对象 FaqListOutBean
。当我使用相同的输入数据执行相同的操作时,从 SoapUI 可以看到有输出。我找不到问题所在。任何建议都会非常有帮助。我从这里开始学习教程:Consuming a SOAP web service.
更新
如果我不使用 JAXBElement
发送请求并直接传递 GetLandingPageFAQ
实例,那么我得到:
org.springframework.ws.client.core.support.WebServiceGatewaySupport.getWebServiceTemplate()
marshalSendAndReceive
Exception in thread "main" org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "GetLandingPageFAQ" as an element because it is missing an @XmlRootElement annotation]
问题出在我使用的网络服务的命名空间和端点上。我检查了网络服务的代码,然后问题得到解决。
我正在使用 string-ws 创建网络服务客户端并从网络服务中获取数据。我使用 org.jvnet.jaxb2.maven2:maven-jaxb2-plugin 来生成 java classes。下面给出了一些由这个插件生成的 classes:
GetLandingPage常见问题解答:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getLandingPageFAQ", propOrder = {
"faq"
})
public class GetLandingPageFAQ {
protected FaqListInBean faq;
public FaqListInBean getFaq() {
return faq;
}
public void setFaq(FaqListInBean value) {
this.faq = value;
}
}
FaqListInBean:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "faqListInBean", propOrder = {
"viewList",
"localList",
"numberOfContent"
})
public class FaqListInBean {
@XmlElement(nillable = true)
protected List<String> viewList;
@XmlElement(nillable = true)
protected List<String> localList;
protected int numberOfContent;
public List<String> getViewList() {
if (viewList == null) {
viewList = new ArrayList<String>();
}
return this.viewList;
}
public List<String> getLocalList() {
if (localList == null) {
localList = new ArrayList<String>();
}
return this.localList;
}
public int getNumberOfContent() {
return numberOfContent;
}
public void setNumberOfContent(int value) {
this.numberOfContent = value;
}
}
GetLandingPageFAQ 响应:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getLandingPageFAQResponse", propOrder = {
"faqListOutBean"
})
public class GetLandingPageFAQResponse {
@XmlElement(name = "FaqListOutBean", namespace = "omitted")
protected FaqListOutBean faqListOutBean;
public FaqListOutBean getFaqListOutBean() {
return faqListOutBean;
}
public void setFaqListOutBean(FaqListOutBean value) {
this.faqListOutBean = value;
}
}
WSDL 是:
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="omitted" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="omitted" name="MyService">
<types>
<xsd:schema>
<xsd:import namespace="omitted" schemaLocation="http://url?xsd=1"/>
</xsd:schema>
</types>
<message name="getLandingPageFAQ">
<part name="parameters" element="tns:getLandingPageFAQ"/>
</message>
<message name="getLandingPageFAQResponse">
<part name="parameters" element="tns:getLandingPageFAQResponse"/>
</message>
<message name="WsException">
<part name="fault" element="tns:WsException"/>
</message>
</message>
<portType name="MyServiceImpl">
<operation name="getLandingPageFAQ">
<input message="tns:getLandingPageFAQ"/>
<output message="tns:getLandingPageFAQResponse"/>
<fault message="tns:WsException" name="WsException"/>
</operation>
</portType>
<binding name="MyServiceImplPortBinding" type="tns:MyServiceImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getLandingPageFAQ">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="WsException">
<soap:fault name="WsException" use="literal"/>
</fault>
</operation>
</binding>
<service name="MyService">
<port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
<soap:address location="http://url/MyService"/>
</port>
</service>
</definitions>
xsd是:
<xs:schema xmlns:tns="omitted" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="omitted">
<xs:element name="WsException" type="tns:WsException"/>
<xs:element name="FaqListInBean" type="tns:faqListInBean"/>
<xs:element name="FaqListOutBean" type="tns:faqListOutBean"/>
<xs:element name="getLandingPageFAQ" type="tns:getLandingPageFAQ"/>
<xs:element name="getLandingPageFAQResponse" type="tns:getLandingPageFAQResponse"/>
<xs:complexType name="faqListInBean">
<xs:sequence>
<xs:element name="viewList" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="localList" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="numberOfContent" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="faqListOutBean">
<xs:sequence>
<xs:element name="faqList" type="tns:simpleFAQOutBean" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="categoryList" type="tns:categoryOutBean" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="numberOfContent" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getLandingPageFAQ">
<xs:sequence>
<xs:element name="faq" type="tns:faqListInBean" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getLandingPageFAQResponse">
<xs:sequence>
<xs:element ref="tns:FaqListOutBean" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="WsException">
<xs:sequence>
<xs:element name="message" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
我通过直接点击 URL 来生成 Java classes,但我没有将 xsd 或 jaxb 绑定文件添加到我的项目中.这是我生成它的方式:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<id>faq--api-service</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generateDirectory>src/main/java</generateDirectory>
<generatePackage>package</generatePackage>
<removeOldOutput>true</removeOldOutput>
<cleanPackageDirectories>true</cleanPackageDirectories>
<schemas>
<schema>
<url>url</url>
</schema>
</schemas>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>1.0.1</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
ObjectFactory class 是:
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _WsException_QNAME = new QName("omitted", "WsException");
private final static QName _FaqListInBean_QNAME = new QName("omitted", "FaqListInBean");
private final static QName _FaqListOutBean_QNAME = new QName("omitted", "FaqListOutBean");
private final static QName _GetLandingPageFAQ_QNAME = new QName("omitted", "getLandingPageFAQ");
private final static QName _GetLandingPageFAQResponse_QNAME = new QName("omitted", "getLandingPageFAQResponse");
public ObjectFactory() {
}
public WsException createWsException() {
return new WsException();
}
public FaqListInBean createFaqListInBean() {
return new FaqListInBean();
}
public FaqListOutBean createFaqListOutBean() {
return new FaqListOutBean();
}
public GetLandingPageFAQ createGetLandingPageFAQ() {
return new GetLandingPageFAQ();
}
public GetLandingPageFAQResponse createGetLandingPageFAQResponse() {
return new GetLandingPageFAQResponse();
}
@XmlElementDecl(namespace = "omitted", name = "WsException")
public JAXBElement<WsException> createWsException(WsException value) {
return new JAXBElement<WsException>(_WsException_QNAME, WsException.class, null, value);
}
@XmlElementDecl(namespace = "omitted", name = "FaqListInBean")
public JAXBElement<FaqListInBean> createFaqListInBean(FaqListInBean value) {
return new JAXBElement<FaqListInBean>(_FaqListInBean_QNAME, FaqListInBean.class, null, value);
}
@XmlElementDecl(namespace = "omitted", name = "FaqListOutBean")
public JAXBElement<FaqListOutBean> createFaqListOutBean(FaqListOutBean value) {
return new JAXBElement<FaqListOutBean>(_FaqListOutBean_QNAME, FaqListOutBean.class, null, value);
}
@XmlElementDecl(namespace = "omitted", name = "getLandingPageFAQ")
public JAXBElement<GetLandingPageFAQ> createGetLandingPageFAQ(GetLandingPageFAQ value) {
return new JAXBElement<GetLandingPageFAQ>(_GetLandingPageFAQ_QNAME, GetLandingPageFAQ.class, null, value);
}
@XmlElementDecl(namespace = "omitted", name = "getLandingPageFAQResponse")
public JAXBElement<GetLandingPageFAQResponse> createGetLandingPageFAQResponse(GetLandingPageFAQResponse value) {
return new JAXBElement<GetLandingPageFAQResponse>(_GetLandingPageFAQResponse_QNAME, GetLandingPageFAQResponse.class, null, value);
}
}
现在我写的客户端是:
public class FaqApiServiceClient extends WebServiceGatewaySupport {
public FaqApiServiceClient() {
try {
String uri = getFaqUri();
setDefaultUri(uri);
} catch (IOException ex) {
throw new ClientInstantiationException(ex);
}
}
public GetLandingPageFAQResponse getLandingPageFAQResponse() {
ObjectFactory objectFactory = new ObjectFactory();
GetLandingPageFAQ faq = new GetLandingPageFAQ();
FaqListInBean bean = objectFactory.createFaqListInBean();
bean.getLocalList().add("en_US");
faq.setFaq(bean);
JAXBElement<GetLandingPageFAQ> request = new ObjectFactory().createGetLandingPageFAQ(faq);
@SuppressWarnings("unchecked")
JAXBElement<GetLandingPageFAQResponse> response = (JAXBElement<GetLandingPageFAQResponse>) getWebServiceTemplate().marshalSendAndReceive(request);
return response.getValue();
}
private String getUri() throws IOException {
InputStream stream = getClass().getClassLoader().getResourceAsStream("wsdl-url.properties");
Properties properties = new Properties();
properties.load(stream);
return properties.getProperty("FaqAPIService.WSDL");
}
}
现在返回的对象 GetLandingPageFAQResponse
不为空,但我找不到子对象 FaqListOutBean
。当我使用相同的输入数据执行相同的操作时,从 SoapUI 可以看到有输出。我找不到问题所在。任何建议都会非常有帮助。我从这里开始学习教程:Consuming a SOAP web service.
更新
如果我不使用 JAXBElement
发送请求并直接传递 GetLandingPageFAQ
实例,那么我得到:
org.springframework.ws.client.core.support.WebServiceGatewaySupport.getWebServiceTemplate()
marshalSendAndReceive
Exception in thread "main" org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "GetLandingPageFAQ" as an element because it is missing an @XmlRootElement annotation]
问题出在我使用的网络服务的命名空间和端点上。我检查了网络服务的代码,然后问题得到解决。