- To create a SOAP webservice, you need to add the JAX-WS dependency to your project
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>${jaxws-rt.version}</version>
</dependency>
- Create an interface to expose the web methods e.g.
package com.arsoft.projects.artutorial.learning.soap.server;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWorldService {
@WebMethod
public String sayHello(String name);
}
- Annotate the interface with @WebService and @SOAPBinding and the exposed method with @WebMethod
- Create the implementation class e.g.
package com.arsoft.projects.artutorial.learning.soap.server;
import javax.jws.WebService;
@WebService(endpointInterface = "com.arsoft.projects.artutorial.learning.soap.server.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {
public String sayHello(String name) {
return "Hello " + name + "!!";
}
}
- Annotate the class with @WebService providing the fully qualified name of the interface as endpointInterface
- Publish the webservice as below
package com.arsoft.projects.artutorial.learning.soap.server;
import javax.xml.ws.Endpoint;
import com.arsoft.projects.artutorial.learning.soap.Constant;
public class SoapServer {
public static void main(String[] args) {
Endpoint.publish(Constant.helloWorldSoapServiceUrl, new HelloWorldServiceImpl());
}
}
- To consume the above webservice, create the stubs from the wsdl by adding below plugin to the pom.xml file of the client application
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>generate-java-sources</id>
<phase>process-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<extension>true</extension>
<wsdlUrls>
<wsdlUrl>http://localhost:8888/ws/hello?wsdl</wsdlUrl>
</wsdlUrls>
</configuration>
</execution>
</executions>
</plugin>
- Add the JAX-WS dependency to the client project <dependency>
- Generate the stub classes by running maven goal 'mvn process-sources. The stub classes will be generated inside folder target/generated-sources. Copy the file to src directory
- Create the consumer class as below
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>${jaxws-rt.version}</version>
</dependency>
package com.odigo.bre.reporting;import java.net.MalformedURLException;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.Service;import com.arsoft.projects.artutorial.learning.soap.server.HelloWorldService;public class SoapClient {public static void main(String[] args) throws MalformedURLException {URL webserviceURL = new URL("http://localhost:8888/ws/hello?wsdl");QName qname = new QName("http://server.soap.learning.artutorial.projects.arsoft.com/","HelloWorldServiceImplService");Service service = Service.create(webserviceURL, qname);HelloWorldService helloWorldService = service.getPort(HelloWorldService.class);String message = helloWorldService.sayHello("Anshul Sood");System.out.println(message);}}
where QName takes targetNamespace and name from wsdl as its arguments.
No comments:
Post a Comment