在基于Spring的Spring客户端中访问通过wsdl公开的API

时间:2014-11-14 19:22:42

标签: java spring rest wsdl

我们如何通过使用spring maven的基于Rest的项目访问其战争部署在同一服务器上的soap项目的wsdl。基本上,我必须访问通过wsdl公开的API,我必须访问此API,响应需要从休息POST方法返回为json。它将像一个REST post方法,接受输入并调用此API(来自wsdl)并将响应操作为JSON,

我必须在没有知识的情况下跳转到WebServices和Spring框架。因此,任何帮助或快速学习这些东西的方向都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

您需要执行以下操作:

  1. 从WSDL中创建客户端代码 这可以通过以下技术在Spring中完成:Spring - Consuming a SOAP service。它将生成从REST API代码调用服务所需的Java类。在这种情况下,您要在同一服务器中调用另一个服务,您只需将端点URL设置为您的服务器。
  2. 创建REST API
    您可以使用Spring MVC设计REST API并调用SOAP服务。您需要使用不同的端点和正确的请求和响应对象开发Controller类。 Spring MVC将使用Jackson框架自动将这些请求和响应对象转换为JSON。使用本指南:Building a RESTful Web Service
  3. 这是从Java REST API中使用SOAP服务的通用方法。如果目标是简单地将SOAP服务公开为REST服务,那么您只需返回从WSDL生成的响应对象。如果是一个选项,我会严格考虑重构SOAP服务代码并将其作为REST API公开。

    注意:在过去的好时光中,直接使用JAX-WS消耗了SOAP,并且通过Jackson完成了JSON对象的暴露。

答案 1 :(得分:0)

Hi I have used the following approach to implement the above requirement:
http://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/
1. changed the pom to add spring-ws dependency and plugin.
2. build the classes and it generated the classes from the wsdl.
3. changed the application xml :
     <!--Generating web sources-->

    <!-- Define the SOAP version used by the WSDL -->
    <bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
        <property name="soapVersion">
            <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
        </property>
    </bean>

    <!-- The location of the generated Java files -->
    <oxm:jaxb2-marshaller id="marshaller" contextPath="com.pb.pims.generatedsources"/>

    <!-- Configure Spring Web Services -->
    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="soapMessageFactory"/>
        <property name="marshaller" ref="marshaller"/>
        <property name="unmarshaller" ref="marshaller"/>
        <property name="defaultUri" value="http://localhost/HSWS/services/HSService?wsdl"/>
    </bean>

4. Created the Service class;

@Service
public class HSService {
    @Autowired
    private WebServiceTemplate webServiceTemplate;


    public List<HSChild> getHSChildren(String hscode, String country,String limit) {

        GetHSChildren getHSChildren= new ObjectFactory().createGetHSChildren();

        getHSChildren.setCountry(country);
        getHSChildren.setHsCode(hscode);
        getHSChildren.setLimit(Integer.parseInt(limit));

        GetHSChildrenResponse response = (GetHSChildrenResponse) webServiceTemplate.marshalSendAndReceive(getHSChildren);

        return response.getGetHSChildrenReturn();
    }


    public static void main(String[] args) {
        ApplicationContext context = new    ClassPathXmlApplicationContext("applicationContext.xml");
        HSService hsService = context.getBean(HSService.class);
    }
}


So, I am able to call this aPI from the wsdl via my client. But I am always getting the values of the getGetHSChildrenReturn. hscode  and getGetHSChildrenReturn.description as null.

Please find below the getGetHSChildrenReturn.class generated in  the Step1 :


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "getHSChildrenReturn"
})
@XmlRootElement(name = "getHSChildrenResponse")
public class GetHSChildrenResponse {

    @XmlElement(required = true)
    protected List<HSChild> getHSChildrenReturn;

    public List<HSChild> getGetHSChildrenReturn() {
        if (getHSChildrenReturn == null) {
            getHSChildrenReturn = new ArrayList<HSChild>();
        }
        return this.getHSChildrenReturn;
    }

Also, I verified in the service code , which we are invoking via this wsdl by putting logging, that the correct request is going and it is returning the expected response at service end. But while coming to the client, the values are set as null.

Please help, what's wrong here in the client side.

Thanks in advance.