Reputation: 287
I am new to soap web-services. I have wsdl file configured in pom.xml like below:
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlUrls>
<wsdlUrl>http://www.webservic.com/uszip.asmx?WSDL</wsdlUrl>
</wsdlUrls>
</configuration>
</execution>
</executions>
</plugin>
I want to use this webservice in spring mvc application. How can I do it?
Upvotes: 1
Views: 172
Reputation: 1764
You can follow this guide for Consuming a SOAP web service on Spring.io.
I prefer following way of consuming web service in Spring. You can inspire yourself. (Note that for generating POJO classes from WSDL I use Eclipse IDE > Generate classes using JAXB).
web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Test WSDL</display-name>
<servlet>
<servlet-name>test-request-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test-request-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
test-request-ws-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ctx="http://www.springframework.org/schema/context" xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<ctx:component-scan base-package="com.test" />
<sws:annotation-driven marshaller="marshaller" unmarshaller="marshaller" />
<sws:static-wsdl id="testRequestWS" location="/WEB-INF/wsdl/test.wsdl" />
<oxm:jaxb2-marshaller id="marshaller">
<oxm:class-to-be-bound name="com.test.request.TestRequest" />
<oxm:class-to-be-bound name="com.test.response.TestResponse" />
</oxm:jaxb2-marshaller>
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>your-project</artifactId>
<name>Your project</name>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
</dependencies>
</project>
TestEndpoint.java
package com.test;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import com.test.request.TestRequest;
import com.test.response.TestResponse;
@Endpoint
public class TestEndpoint {
@PayloadRoot(namespace = "http://test.com/wsdl-namespace", localPart = "testRequest")
@ResponsePayload
public TestResponse test(@RequestPayload TestRequest request) {
final TestResponse res = new TestResponse();
res.setId(1);
return res;
}
}
Upvotes: 1