Spring Web Services using JAXB part-2

After the part-1 this is the contd part of it, now we are going to write the endpoint implementations and mapping these end points to the request from the web. so lets not waste time just jumping into the implementation part here we go.

Lets write AbstractPersonEndPoint class which will extend AbstractMarshallingPayloadEndpoint of Spring framework. Here we are going to just create PersonService object and define an abstract method invokeInternal of AbstractMarshallingPayloadEndpoint class.

/**
 * 
 */
package com.spark.springws.abstrct.endpoint;

import org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint;

import com.spark.springws.service.PersonService;

/**
 * @author Sony
 *
 */
public abstract class AbstractPersonEndPoint extends AbstractMarshallingPayloadEndpoint {

	protected PersonService personService;
	
	/**
	 * @param personService the personService to set
	 */
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}


	/* (non-Javadoc)
	 * @see org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint#invokeInternal(java.lang.Object)
	 */
	@Override
	protected abstract Object invokeInternal(Object request) throws Exception;

}

Now its time to create concrete endpoint classes which will have exact business logic implementation and will extend AbstractPersonEndPoint. I will show for one of the service and other implementations are left for you.

/**
 * 
 */
package com.spark.springws.endpoints;

import org.springws.beans.schema.AddPersonRequest;

import com.spark.springws.abstrct.endpoint.AbstractPersonEndPoint;

/**
 * @author Sony
 *
 */
public class AddPersonEndPoint extends AbstractPersonEndPoint {

	/* (non-Javadoc)
	 * @see com.spark.springws.abstrct.endpoint.AbstractPersonEndPoint#invokeInternal(java.lang.Object)
	 */
	@Override
	protected Object invokeInternal(Object request) throws Exception {

		AddPersonRequest addPersonRequest = (AddPersonRequest) request;
		personService.addPerson(addPersonRequest.getPerson());
		return null;
	}

}

now lets take a look at the spring-ws-servlet.xml which we already defined in this part-1 section and modify it a little bit as shown below.

<?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:oxm="http://www.springframework.org/schema/oxm"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd">

	<bean id="personService" class="com.spark.springws.service.impl.PersonServiceImpl"
		init-method="initialize" />
	<bean id="Person"
		class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
		<property name="schema" ref="schema" />
		<property name="portTypeName" value="Person" />
		<property name="locationUri" value="/services" />
		<property name="targetNamespace" value="http://www.example.org/person/schema" />
	</bean>
	<bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
		<property name="xsd" value="/WEB-INF/xsd/schemas/Person.xsd" />
	</bean>

	<bean id="validationInterceptor"
		class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
		<property name="xsdSchema" ref="schema"></property>
		<property name="validateRequest" value="true" />
		<property name="validateResponse" value="true" />
	</bean>

	<bean id="loggingInterceptor"
		class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />

	<oxm:jaxb2-marshaller id="marshaller"
		contextPath="org.springws.beans.schema" />
	<oxm:jaxb2-marshaller id="unmarshaller"
		contextPath="org.springws.beans.schema" />

	<bean id="addPersonEndpoint" class="com.spark.springws.endpoints.AddPersonEndPoint"
		autowire="byName" />
	<bean id="getAllPersonsEndpoint" class="com.spark.springws.endpoints.GetAllPersonsEndPoint"
		autowire="byName" />
	<bean id="getPersonEndpoint" class="com.spark.springws.endpoints.GetPersonEndPoint"
		autowire="byName" />

	<bean name="endpointMapping"
		class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
		<property name="interceptors">
			<list>
				<ref local="loggingInterceptor" />
				<ref local="validationInterceptor" />
			</list>
		</property>
		<property name="mappings">
			<props>
				<prop key="{http://www.example.org/person/schema}AddPersonRequest">addPersonEndpoint</prop>
				<prop key="{http://www.example.org/person/schema}GetAllPersonsRequest">getAllPersonsEndpoint</prop>
				<prop key="{http://www.example.org/person/schema}GetPersonRequest">getPersonEndpoint</prop>
				<prop key="{http://www.example.org/person/schema}UpdatePersonRequest">updatePersonEndpoint</prop> 
					<prop key="{http://www.example.org/person/schema}DeletePersonRequest">deletePersonEndpoint</prop>
			</props>
		</property>
	</bean>

	<bean id="exceptionResolver"
		class="org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver">
		<property name="defaultFault" value="SERVER" />
		<property name="exceptionMappings">
			<props>
				<prop key="org.springframework.oxm.ValidationFailureException">CLIENT,Invalid request</prop>
				<prop key="com.spark.springws.custom.exception.PersonException">SERVER</prop>
			</props>
		</property>
	</bean>
</beans>

once this is done just deploy the project and test the request and response from SOAP UI as shown below.
add-request

get-req-res

Happy Spring-Web services šŸ™‚

Advertisements

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