Skip navigation
2011

Why Spring WS and JAXB?

 

  • Spring WS supports contract-first web services.  Contract-first web services promotes loose coupling between contracts and implementations. Such a looser coupling allows companies to change implementations without worrying about affecting existing contracts.
  • JAXB bridges the gab between Java and xml. JAXB implements the marshalling and un-marshalling functions so that you don’t have to implement xml and java mapping.

 

Steps to develop Spring WS with JAXB:

 

 

  • Create the data contract - xml schema file
  • Generate the JAXB Java classes from the xml schema file
  • Create Java Endpoint classes
  • Define and wire Endpoint in Spring context file
  • Compile, package and deploy the code to Tomcat
  • Test the code: “http://localhost:8080/holiday/holiday.wsdl”

 

 

Create the data contract

 

 

 

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema targetNamespace="http://mycompany.hr.com/hr/v1" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">

<xs:element name="HolidayRequest">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="EmployeeName" type="xs:string"/>

    <xs:element name="StartDate" type="xs:date"/>

        <xs:element name="EndDate" type="xs:date"/>

   </xs:sequence>

  </xs:complexType>

</xs:element>

<xs:element name="HolidayResponse">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="Answer" type="xs:string"/>

   </xs:sequence>

  </xs:complexType>

</xs:element>

</xs:schema>

 

 

  • This  service exposes “HolidayRequest” and returns “HolidayResponse”

 

 

 

Generate JAXB Java classes from the schema file

 

 

Executing “xjc -p com.mycompany.hr.jaxb.holidayService hr.xsd” will generate the following classes:

 

  • com\mycompany\hr\jaxb\holidayService\HolidayRequest.java
  • com\mycompany\hr\jaxb\holidayService\HolidayResponse.java
  • com\mycompany\hr\jaxb\holidayService\ObjectFactory.java
  • com\mycompany\hr\jaxb\holidayService\package-info.java

 

 

Create Java Endpoint

 

 

package com.mycompany.hr.ws;

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import com.mycompany.hr.jaxb.holidayService.HolidayRequest;
import com.mycompany.hr.jaxb.holidayService.HolidayResponse;

@Endpoint
public class HolidayEndpoint {

 

@PayloadRoot(localPart= "HolidayRequest", namespace="http://mycompany.hr.com/hr/v1")
  public HolidayResponse serveRequest(HolidayRequest request){
   HolidayResponse  response = new HolidayResponse();
     response.setAnswer("Approved");
  return response;
}

}

 

  • Spring @PayloadRoot annotation instructs Spring to map xml “HolidayRequest” to “serveRequest” Java method. 
  • The serveRequest method returns a HolidayResponse object

 

 

Define and wire Endpoint in Spring

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  <!-- Add automatic WSDL generation support -->
    <bean id="holiday" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
   <property name="schema" ref="schema" />
  <property name="portTypeName" value="holiday" />
  <property name="locationUri" value="http://localhost:8080/holiday/" />
  <property name="targetNamespace" value="http://mycompany.hr.com/hr/v1" />
    </bean> 

  <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
<property name="xsd" value="/WEB-INF/hr.xsd" />
    </bean>

<!-- Register PayloadRootAnnotationMethodEndpointMapping -->
    <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping" /> 

    <!-- Register Endpoint -->
    <bean id="HolidayEndpoint" class="com.mycompany.hr.ws.HolidayEndpoint">
    <description>
            This endpoint handles requests.
        </description>
    </bean> 

 

<!-- Configure XML Marshaller -->
<bean class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
    <constructor-arg ref="marshaller" />
</bean> 

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.mycompany.hr.jaxb.holidayService.HolidayRequest</value>
            <value>com.mycompany.hr.jaxb.holidayService.HolidayResponse</value>
        </list>
    </property>
</bean>
</beans>

 

 

  • Spring will generate the WSDL from the schema file automatically
  • Spring bean “HolidayEndpoint” defines the end point mapping
  • Spring bean  “marshaller” un-marshalls the xml requests to Java objects and marshalls the response objects to xml response

 


Source code and war files are attached.

 

Deploy the code to Tomcat and let me what happened

Filter Blog

By date:
By tag: