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

Java 在基于SpringREST的客户端中访问通过wsdl公开的API,java,spring,rest,wsdl,Java,Spring,Rest,Wsdl,我们如何通过使用SpringMaven的基于Rest的项目访问其war部署在同一服务器上的soap项目的wsdl。基本上,我必须访问一个通过wsdl公开的API,并且我必须访问这个API,响应需要作为json从rest POST方法返回。它将类似于REST post方法,接受输入并调用此API(来自wsdl)并将响应作为JSON进行操作 我必须跳入WebServices和Spring框架,而不需要通过知识。因此,任何快速学习这些知识的帮助或指导都将不胜感激。您需要执行以下操作: 从WSDL创建客

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


我必须跳入WebServices和Spring框架,而不需要通过知识。因此,任何快速学习这些知识的帮助或指导都将不胜感激。

您需要执行以下操作:

  • 从WSDL创建客户机代码
    这可以通过以下技术在Spring中完成:。它将生成从RESTAPI代码调用服务所需的Java类。在这种情况下,您正在调用同一服务器中的另一个服务,您所要做的就是将端点url设置为您的服务器
  • 创建您的REST API
    您可以使用SpringMVC设计RESTAPI并调用SOAP服务。您将需要开发一个具有不同端点和适当的请求和响应对象的控制器类。SpringMVC将使用Jackson框架自动将这些请求和响应对象转换为JSON。使用本指南:
  • 这是从JavaRESTAPI使用SOAP服务的一般方式。如果目标是简单地将SOAP服务公开为REST服务,那么您可以返回从WSDL生成的响应对象。如果它是一个选项,我将认真考虑重构SOAP服务代码并将其作为REST API公开。 注意:在过去的好日子里,使用SOAP是通过直接使用JAX-WS完成的,而暴露JSON对象是通过Jackson完成的。

    Hi我使用了以下方法来实现上述要求:
    
    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.
    
    http://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/ 1.更改pom以添加SpringWS依赖项和插件。 2.构建类,并从wsdl生成类。 3.已更改应用程序xml: 4.创建服务类; @服务 公营服务{ @自动连线 私有WebServiceTemplate WebServiceTemplate; 公共列表getHSChildren(字符串hscode、字符串国家/地区、字符串限制){ GetHSChildren GetHSChildren=newObjectFactory().createGetHSChildren(); getHSChildren.setCountry(国家); getHSChildren.setHsCode(hscode); getHSChildren.setLimit(Integer.parseInt(limit)); GetHSChildrenResponse=(GetHSChildrenResponse)webServiceTemplate.MarshallSendAndReceive(GetHSChildrenResponse); return response.getGetHSChildrenReturn(); } 公共静态void main(字符串[]args){ ApplicationContext context=new ClassPathXmlApplicationContext(“ApplicationContext.xml”); HSService HSService=context.getBean(HSService.class); } } 因此,我能够通过我的客户机从wsdl调用这个aPI。但是我总是得到gethschildrenreturn的值。hscode和gethschildrenreturn.description为null。 请在下面找到步骤1中生成的GetHsChildrenReturn.class: @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name=),比例={ “getHSChildrenReturn” }) @XmlRootElement(name=“getHSChildrenResponse”) 公共类GetHSChildrenResponse{ @XmlElement(必需=true) 受保护列表getHSChildrenReturn; public List gethschildrenreturn(){ if(getHSChildrenReturn==null){ getHSChildrenReturn=newarraylist(); } return this.getHSChildrenReturn; } 此外,我在服务代码中验证了正确的请求正在进行,并且它在服务端返回了预期的响应。但是,当到达客户端时,这些值被设置为null。 请帮忙,客户端出了什么问题。 提前谢谢。