Skip navigation

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="" xmlns:xs="" elementFormDefault="unqualified" attributeFormDefault="unqualified">

<xs:element name="HolidayRequest">



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

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

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




<xs:element name="HolidayResponse">



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







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




Generate JAXB Java classes from the schema file



Executing “xjc -p hr.xsd” will generate the following classes:


  • com\mycompany\hr\jaxb\holidayService\
  • com\mycompany\hr\jaxb\holidayService\
  • com\mycompany\hr\jaxb\holidayService\
  • com\mycompany\hr\jaxb\holidayService\



Create Java Endpoint





public class HolidayEndpoint {


@PayloadRoot(localPart= "HolidayRequest", namespace="")
  public HolidayResponse serveRequest(HolidayRequest request){
   HolidayResponse  response = new HolidayResponse();
  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="" xmlns:xsi=""

  <!-- Add automatic WSDL generation support -->
    <bean id="holiday" class="">
   <property name="schema" ref="schema" />
  <property name="portTypeName" value="holiday" />
  <property name="locationUri" value="http://localhost:8080/holiday/" />
  <property name="targetNamespace" value="" />

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

<!-- Register PayloadRootAnnotationMethodEndpointMapping -->
    <bean class="" /> 

    <!-- Register Endpoint -->
    <bean id="HolidayEndpoint" class="">
            This endpoint handles requests.


<!-- Configure XML Marshaller -->
<bean class="">
    <constructor-arg ref="marshaller" />

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">



  • 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: