简体   繁体   中英

Java Web Services framework

I have the following scenario: I have to implement web services client that will serve different web services providers with various wsdl-s. Even the same provider can have different versions of wsdl-s that I'll have to support. We are using java with spring, therefore I was thinking about spring-ws framework. But it seems, that as part of implementation, I have to import wsdl into my project and to write code based on created as part of import pojo-s. So, no problem with different code for different providers, since the logic is different. But I prefer to have the same code for different versions of the same provider and to avoid creation of different versions of pojo-s. So, I'm looking for something similar to suds library for python. There you just provide particular wsdl and make a call. You don't deal with wsdl import and different versions of wsdl of the same provider - just need to adjust parameter list of ws call if needed. Preferable, if spring-ws can do something similar - maybe I just didn't realize that, since now we are trying to get rid of Axis and Axis-2.

Did you take a look at the webServiceTemplate ? You have a lot of possibilities and no you do not need to import the wsdl each time. For example you could use a string like this:

StreamSource source = new StreamSource(new StringReader(....));
ByteArrayOutputStream  bytArrayOutputStream = new ByteArrayOutputStream();
StreamResult result = new StreamResult(bytArrayOutputStream);
wsTemplate.sendSourceAndReceiveToResult(source, result); 
final String reply = new String(_bytArrayOutputStream.toByteArray());

However this solution is not appropriate on the long run. I will suggest rather using pojos like this

1) In the spring configuration,

<bean id="wsTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <property name="defaultUri" value="...."/>
    <property name="marshaller" ref="marshaller"/>
    <property name="unmarshaller" ref="marshaller"/>
</bean> 

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.mycompany.app.ws"/>
</bean>

2) Create package com.mycompany.app.ws and add a package info

@javax.xml.bind.annotation.XmlSchema(
        namespace = "....")
package com.mycompany.app.ws;

3) inside the package com.mycompany.app.ws, you will need to define the request and response.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {})
@XmlRootElement(name = "WsRequest")
public final class WsRequest{
    @XmlElement(required = true)
    private String requstData;
}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {})
@XmlRootElement(name = "WsResponse")
public final class WsResponse {
    @XmlElement(required = true)
    private String responseData;
}

4) In your code do the following:

WsResponse wsResponse = (WsResponse ) 
 wsTemplate.marshalSendAndReceive(wsRequest_);

What happens if you need to add a new wsdl or new provider ?

  • You can reuse the pojos : You can create another package (for the namespace change) and create a class wsRequest2 that extends wsRequest
  • You need to define a new wsTemplate and marshaller in the spring configuration

I think also that you could create a factory that initializes the wsTemplate with the appropriate marshaller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM