Reputation: 29816
I am using string-ws to create webservice client and fetching data from the webservice. I have used org.jvnet.jaxb2.maven2:maven-jaxb2-plugin to generate the java classes. Some of the classes are given below generated by this plugin:
GetLandingPageFAQ:
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;
}
}
GetLandingPageFAQResponse:
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;
}
}
The WSDL is:
<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>
The xsd is:
<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>
I am generating the Java classes by hitting the URL directly and I don't have the xsd or the jaxb binding file added to my project. Here is how I am generating it:
<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>
The ObjectFactory class is:
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);
}
}
Now the client that I have written is:
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");
}
}
Now the object GetLandingPageFAQResponse
which is returing is not null but I cannot find the child objects FaqListOutBean
. From SoapUI when I am executing the same operating with same input data I can see there is output. I am unable to find what is wrong. Any suggestion would be very helpful. I have followed the tutorial from here: Consuming a SOAP web service.
If I don't use the JAXBElement
to send the request and pass the GetLandingPageFAQ
instance directly then I am getting:
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]
Upvotes: 1
Views: 2738
Reputation: 29816
The issue is with the namespace & endpoint of the webservice that I was consuming. I checked the code of the webservice and then the issue got fixed.
Upvotes: 1