5 minutes with – Spring Web Services

In these little posts “5 minutes with” I’d like to show you a very little part of code to complete a common pattern.

All the arguments that I’ll show are very wide, so, if you want to find out more about this, my suggestion is to follow the official web site.

Let’s start with build Web Services with Spring .

I love Spring Framework, really, I think it’s very useful, easy to use and good standard quality for Java developer.

In this post I’m going to show how build a simple Web Service with Spring and Cxf .

First, my libraries included:

aopalliance-1.0.jar
asm-2.2.3.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
cxf-api-2.2.6.jar
cxf-common-schemas-2.2.6.jar
cxf-common-utilities-2.2.6.jar
cxf-rt-bindings-soap-2.2.6.jar
cxf-rt-bindings-xml-2.2.6.jar
cxf-rt-core-2.2.6.jar
cxf-rt-databinding-jaxb-2.2.6.jar
cxf-rt-frontend-jaxws-2.2.6.jar
cxf-rt-frontend-simple-2.2.6.jar
cxf-rt-transports-http-2.2.6.jar
cxf-rt-ws-addr-2.2.6.jar
cxf-tools-common-2.2.6.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.6.jar
geronimo-jaxws_2.1_spec-1.0.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.12.jar
lib.txt
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
spring-aop-3.0.1.RELEASE.jar
spring-asm-3.0.1.RELEASE.jar
spring-beans-3.0.1.RELEASE.jar
spring-context-3.0.1.RELEASE.jar
spring-core-3.0.1.RELEASE.jar
spring-expression-3.0.1.RELEASE.jar
spring-web-3.0.1.RELEASE.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.9.jar
xml-resolver-1.2.jar
XmlSchema-1.4.5.jar

I used Spring version 3.0.1 and Cxf 2.2.6. Take attention at this versions because I’ve got some problems when the libraries were in different versions.

Then, I defined the interface of my service

package it.sample.samplewebservicespring.service;
import it.sample.samplewebservicespring.bean.Speak;
import javax.jws.WebParam;
import javax.jws.WebParam.Mode;
import javax.jws.WebService;
@WebService
public interface HelloService {
 Speak speakHello(@WebParam(name="message",
        mode=Mode.IN)
        Speak message);
}

And its implementation

package it.sample.samplewebservicespring.service;
import it.sample.samplewebservicespring.bean.Speak;
import javax.jws.WebService;
@WebService(endpointInterface = "it.sample.samplewebservicespring.service.HelloService")
public class HelloServiceImpl implements HelloService{
 @Override
 public Speak speakHello(Speak message) {
  
  switch(message.getLanguage())
  {
   case IT:
    message.setOriginalMessage("Ciao");
    break;
   case EN:
    message.setOriginalMessage("Hello");
    break;
   case FR:
    message.setOriginalMessage("Bonjour");
    break;
   case DE:
    message.setOriginalMessage("Hallo");
    break;
  } 
  return message;
 }
 }

The service receives a Speak object type object with language field and returns the same object type with the “Hello” message in the selected language.

package it.sample.samplewebservicespring.bean;
import it.sample.samplewebservicespring.service.Language;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "speak", propOrder = {
    "language",
    "originalMessage"
})
public class Speak {
    @XmlElement(required = true)
    protected Language language;
    protected String originalMessage;
    public Language getLanguage() {
        return language;
    }
    public void setLanguage(Language value) {
        this.language = value;
    }
    public String getOriginalMessage() {
        return originalMessage;
    }
    public void setOriginalMessage(String value) {
        this.originalMessage = value;
    }
}

Web.xml will be configurated as follow

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>CXF Spring Web Service</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>CXFSpringServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFSpringServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

And in my application context I defined the jaxws endpoint. It looks like this

<?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:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

   <bean id="hello" class="it.sample.samplewebservicespring.service.HelloServiceImpl" />

	<jaxws:endpoint id="helloService" implementor="#hello" address="/HelloService" />

</beans>

Now, it’s time to run!

When you start your web application (I used Tomcat) you can see at http://localhost:8080/SampleWebServiceSpring/HelloService?wsdl your wsdl generated.

From a Soap Client (I used SoapUI) the request message should be something like this

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.samplewebservicespring.sample.it/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:speakHello>
         <!--Optional:-->
         <message>
            <language>IT</language>
            <!--Optional:-->
            <originalMessage>?</originalMessage>
         </message>
      </ser:speakHello>
   </soapenv:Body>
</soapenv:Envelope>

And the response message will be

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:speakHelloResponse xmlns:ns2="http://service.samplewebservicespring.sample.it/">
         <return>
            <language>IT</language>
            <originalMessage>Ciao</originalMessage>
         </return>
      </ns2:speakHelloResponse>
   </soap:Body>
</soap:Envelope>

That’s all. From this point you can expand your web service using Cxf configuration. The references are here

Advertisements

One thought on “5 minutes with – Spring Web Services

  1. Pingback: Fix Aopalliance-1.0.jar Errors - Windows XP, Vista, 7 & 8

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s