简体   繁体   English

Java Web服务框架

[英]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. 我有以下情形:我必须实现Web服务客户端,该客户端将使用各种wsdl-s为不同的Web服务提供者提供服务。 Even the same provider can have different versions of wsdl-s that I'll have to support. 即使是同一提供程序,也可能具有我必须支持的不同版本的wsdl-s。 We are using java with spring, therefore I was thinking about spring-ws framework. 我们在Spring中使用Java,因此我在考虑spring-ws框架。 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. 但是似乎,作为实现的一部分,我必须将wsdl导入到我的项目中,并根据基于import pojo-s的created编写代码。 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. 但是我更喜欢为同一提供者的不同版本使用相同的代码,并避免创建不同版本的pojo-s。 So, I'm looking for something similar to suds library for python. 因此,我正在寻找类似于python的suds库的东西。 There you just provide particular wsdl and make a call. 在那里,您只需提供特定的wsdl并进行呼叫。 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. 您无需处理wsdl导入和同一提供程序的不同版本的wsdl-只需在需要时调整ws调用的参数列表。 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. 最好是,如果spring-ws可以做类似的事情-也许我只是没有意识到,因为现在我们正在尝试摆脱Axis和Axis-2。

Did you take a look at the webServiceTemplate ? 您是否看过webServiceTemplate You have a lot of possibilities and no you do not need to import the wsdl each time. 您有很多可能性,并且不需要每次都导入wsdl。 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 我会建议使用像这样的pojos

1) In the spring configuration, 1)在弹簧配置中,

<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 2)创建包com.mycompany.app.ws并添加包信息

@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. 3)在包com.mycompany.app.ws中,您将需要定义请求和响应。

@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: 4)在您的代码中执行以下操作:

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

What happens if you need to add a new wsdl or new provider ? 如果您需要添加新的wsdl或新的提供程序,会发生什么?

  • You can reuse the pojos : You can create another package (for the namespace change) and create a class wsRequest2 that extends wsRequest 您可以重用pojos:您可以创建另一个包(用于名称空间更改),并创建扩展wsRequest的类wsRequest2。
  • You need to define a new wsTemplate and marshaller in the spring configuration 您需要在spring配置中定义一个新的wsTemplate和marshaller

I think also that you could create a factory that initializes the wsTemplate with the appropriate marshaller. 我还认为您可以创建一个工厂来使用适当的编组器初始化wsTemplate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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