A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Tuesday, December 4, 2012

JAX-WS Web Service with Spring

10:11:00 PM Posted by Satish , , , , , , , , No comments
I am going to demonstrate how to create a web service with Spring. For this tutorial I will be using the following tool.
  • JDK 7
  • Eclipse Juno
  • Maven2
  • Tomcat 7
Before start coding let's create a java project using the following maven command.

1
mvn archetype:generate -DgroupId=com.techiekernel -DartifactId=webservice-JAX-WS-Spring -Dpackagename=com.techiekernel -DarchetypeArtifactId=maven-archetype-webapp

After you execute, the project will be get created with pom.xml file. As I am using JDK 7 and annotations, I have to specify the updated maven plugin. And I have to specify the dependency for GlassFish Metro and Spring . So the final pom.xml is shown bellow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.techiekernel</groupId>
  <artifactId>webservice-JAX-WS-Spring</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>webservice-JAX-WS-Web Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <repositories>
    <repository>
      <id>java.net</id>
      <url>http://download.java.net/maven/2</url>
    </repository>
  </repositories>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring framework -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring</artifactId>
      <version>2.5.6</version>
    </dependency>

    <!-- JAX-WS -->
    <dependency>
      <groupId>com.sun.xml.ws</groupId>
      <artifactId>jaxws-rt</artifactId>
      <version>2.2.3</version>
    </dependency>

    <!-- Library from java.net, integrate Spring with JAX-WS -->
    <dependency>
      <groupId>org.jvnet.jax-ws-commons.spring</groupId>
      <artifactId>jaxws-spring</artifactId>
      <version>1.8</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.sun.xml.stream.buffer</groupId>
          <artifactId>streambuffer</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.jvnet.staxex</groupId>
          <artifactId>stax-ex</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
    <finalName>webservice-JAX-WS-Spring</finalName>
  </build>
</project>

We are going to do the following thing in this tutorial.
  • Web Service End Point
  • Service Interface and Class
  • Front Controller Configuration
  • Spring Mapping for web service and Service Integration
Web Service End Point:

We are going to create a web service end point class and create a member of service interface to get the object injected by the spring.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.techiekernel.ws.jaxws;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.techiekernel.ws.jaxws.document.FooBar;
import com.techiekernel.ws.jaxws.document.Server;

@WebService
public class FooBarWebService {
  private FooBar fooBar;

  @WebMethod(exclude=true)
  public void setFooBar(FooBar fooBar) {
    this.fooBar = fooBar;
  }
  
  @WebMethod(operationName="callFooBar")
  public String callFooBar(String name){
    return fooBar.callFooBar(name);
  }
  
  @WebMethod(operationName="getServerDetail")
  public Server getServerDetail(String client){
    return fooBar.getServerDetail(client);
  }
}

Service Interface and Class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package com.techiekernel.ws.jaxws.document;

/**
 * Service end point interface
 * @author satish
 *
 */
public interface FooBar {
  String callFooBar(String name);
  
  Server getServerDetail(String client);
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package com.techiekernel.ws.jaxws.document;



public class FooBarImpl implements FooBar{

  public String callFooBar(String name) {
    // TODO Auto-generated method stub
    return "FooBar called by " + name;
  }

  public Server getServerDetail(String client) {
    // TODO Auto-generated method stub
    Server server = new Server();
    server.setName("Techie Kernel");
    server.setIp("192.168.1.0");
    server.setMac("12-75-61-09-12-22");
    server.setOs("Ubuntu");
    return server;
  }
}

As one of the WebMethod is returning a instance of Server. we have to create the following Server class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.techiekernel.ws.jaxws.document;

public class Server {
  private String name;
  private String ip;
  private String mac;
  private String os;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getIp() {
    return ip;
  }
  public void setIp(String ip) {
    this.ip = ip;
  }
  public String getMac() {
    return mac;
  }
  public void setMac(String mac) {
    this.mac = mac;
  }
  public String getOs() {
    return os;
  }
  public void setOs(String os) {
    this.os = os;
  }
  @Override
  public String toString() {
    return "Server [name=" + name + ", ip=" + ip + ", mac=" + mac + ", os="
        + os + ", toString()=" + super.toString() + "]";
  }
}

Front Controller Configuration:

We have to configure the front controller in web.xml file and the deployment descriptor looks some thing like this after that.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>webservice-JAX-Spring</display-name>
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <servlet>
    <servlet-name>jaxws-servlet</servlet-name>
    <servlet-class>
      com.sun.xml.ws.transport.http.servlet.WSSpringServlet
    </servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>jaxws-servlet</servlet-name>
    <url-pattern>/foobar</url-pattern>
  </servlet-mapping>
</web-app>

Spring Mapping for web service and Service Integration:

We have to create applicationContext.xml in class path with all the bean definitions. So that spring will read the configuration to provide the infrastructure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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:ws="http://jax-ws.dev.java.net/spring/core"
       xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://jax-ws.dev.java.net/spring/core
        http://jax-ws.dev.java.net/spring/core.xsd
        http://jax-ws.dev.java.net/spring/servlet
        http://jax-ws.dev.java.net/spring/servlet.xsd"
>
 
    <wss:binding url="/foobar">
        <wss:service>
            <ws:service bean="#fooBarWs"/>
        </wss:service>
    </wss:binding>
 
    <!-- Web service methods -->
    <bean id="fooBarWs" class="com.techiekernel.ws.jaxws.FooBarWebService">
      <property name="fooBar" ref="fooBar" />
    </bean>
 
    <bean id="fooBar" class="com.techiekernel.ws.jaxws.document.FooBarImpl" />
 
</beans>

Now it's time to deploy the application in tomcat. After deployment, we can access the WSDL with the following URL.


1
http://localhost:8080/webservice-JAX-WS-Spring/foobar?wsdl


Source Code:

You can pull the source code from GitHub.

0 comments:

Post a Comment