2 minute read

This is a simple example of webservices using jax-ws spring component.
It is a contact application that allows you to store your addresses.
The webservice retrieves the list of contacts.

web.xml
 <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>/ContactAppService</url-pattern>
</servlet-mapping>
springwebapp-servlet.xml
 <!-- use component-scan and annotations -->  
<context:component-scan base-package="com.mycompany" />
<mvc:annotation-driven />
applicationContext.xml
setup bean definitions use jax-ws spring components
 <beans xmlns:ws="http://jax-ws.dev.java.net/spring/core">http://jax-ws.dev.java.net/spring/core"  
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet">http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation="
http://jax-ws.dev.java.net/spring/core">http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd">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
http://jax-ws.dev.java.net/spring/servlet.xsd">http://jax-ws.dev.java.net/spring/servlet.xsd
">
<bean id="simplecontactApp" class="com.mycompany.SimpleContactAppImpl">
<property name="contactDao">
<ref bean="contactDao" />
</property>
</bean>
<bean id="wsLoggingHandler" class="com.mycompany.common.LoggingHandler">
</bean>
<wss:binding url="/ContactAppService">
<wss:service>
<ws:service bean="#simplecontactApp">
<ws:handlers>
<ref bean="wsLoggingHandler" />
</ws:handlers>
</ws:service>
</wss:service>
</wss:binding>
</beans>
java interface code
SimpleContactApp.java
 package com.mycompany;  
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* Exposing web services using JAX-WS
* @author ming
*
*/
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL)
public interface SimpleContactApp {
@WebResult(name="return")
public Contact getContact(String firstName);
public void insertAcontact(Contact contact);
public String sayHello();
}
java implementation code
SimpleContactAppImpl.java
 package com.mycompany;  
import javax.jws.WebService;
import com.mycompany.dao.ContactDAO;
/**
* This is a simple webservice can be deployed along with a webapp
* remember to use simple data types
* webservices interoperability simple data types
* to access http://localhost:8080/ContactAppService?WSDL
* @author ming
*
*/
@WebService(serviceName="ContactAppService", portName="ContactService",
endpointInterface = "com.mycompany.SimpleContactApp")
public class SimpleContactAppImpl implements SimpleContactApp {
private ContactDAO contactDao;
@Override
public Contact getContact(String firstName) {
return contactDao.getContact(1);
}
@Override
public void insertAcontact(Contact contact) {
contactDao.insertContact(contact);
}
public ContactDAO getContactDao() {
return contactDao;
}
public void setContactDao(ContactDAO contactDao) {
this.contactDao = contactDao;
}
@Override
public String sayHello() {
return "hello to you too";
}
}
jars
Download the jax-ws reference implementation from java.net:
http://jax-ws.java.net/
extract and get the jars
jaxb-api.jar (We use jaxb for the object to xml bindings.)
jaxb-impl.jar
jaxws-api.jar
jaxws-rt.jar
ha-api.jar
gmbal-api-only.jar
management-api.jar
policy.jar
stax-ex.jar
streambuffer.jar
xbean-spring-2.7.jar
(Include spring and hibernate and database jars.)
jaxws-spring-1.8.jar (Download from the jax-ws commons spring section)
Put all jars in WEB-INF/lib

Simple Client App
ContactWebserviceClient
 package com.mycompany.client;  
import java.util.Collection;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mycompany.Contact;
import com.mycompany.SimpleContactApp;
public class ContactWebserviceClient {
private SimpleContactApp service;
/**
* @param args
*/
public static void main(String[] args) {
try {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("com/mycompany/client/contactclient.xml");
ContactWebserviceClient conapp = (ContactWebserviceClient)
ctx.getBean("client");
Contact ch = conapp.getService().getContact("contact1");
System.out.println((ch == null) ? "contact is null add some
test data" : "contact firstname " + ch.getFirstName());
System.out.println(conapp.getService().sayHello());
} catch (BeansException e) {
e.printStackTrace();
}
}
public SimpleContactApp getService() {
return service;
}
public void setService(SimpleContactApp service) {
this.service = service;
}
}
With contactclient.xml
 <bean id="client" class="com.mycompany.client.ContactWebserviceClient">  
<property name="service" ref="contactAppWebService"/>
</bean>
<bean id="contactAppWebService"
class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="com.mycompany.SimpleContactApp"/>
<property name="wsdlDocumentUrl"
value="http://localhost:8080/springwebapp/ContactAppService?WSDL"/>
<property name="namespaceUri" value="http://mycompany.com/"/>
<property name="serviceName" value="ContactAppService"/>
<property name="portName" value="ContactService"/>
</bean>

Comments