This recipe demonstrates a very simple endpoint mapping that maps a Web-Service request to a Java class method.
In this recipe, the project's name is LiveRestaurant_R-1.9
, with the following Maven dependencies:
spring-ws-core-2.0.1.RELEASE.jar
log4j-12.9.jar
The steps of this recipe are the same as that of the previous recipe, Setting up a contract-first Web-Service, except that the registration of the endpoint, that is, method endpoint mapping and is configured in spring-ws-servlet.xml
.
Define an endpoint (
OrderSeviceMethodEndpoint
) based on the method mapping standard (SimpleMethodEndpointMapping
).Configure the method endpoint mapping in
spring-ws-servlet.xml
.Run the
mvn clean package tomcat:run
command and browse to see the WSDL:http://localhost:8080/LiveRestaurant/OrderService.wsdl
To test, open a new command window, go to
Liverestaurant_R-1.9-Client
, and run the following command:mvn clean package exec:java
Here is the server-side output:
Sent response .. <tns:placeOrderResponse xmlns:tns="..."><tns:refNumber>order-John_Smith_1234</tns:refNumber> </tns:placeOrderResponse>... for request ... <tns:placeOrderRequest xmlns:tns="..."> <tns:order> <tns:refNumber>order-9999</tns:refNumber> <tns:customer> ........ </tns:customer> <tns:dateSubmitted>2008-09-29T05:49:45</tns:dateSubmitted> <tns:orderDate>2014-09-19T03:18:33</tns:orderDate> <!--1 or more repetitions:--> <tns:items> <tns:type>Snacks</tns:type> <tns:name>Pitza</tns:name> <tns:quantity>2</tns:quantity> </tns:items> </tns:order> </tns:placeOrderRequest>
SimpleMethodEndpointMapping
maps from the local name of the request payload (placeOrderRequest
) to the methods of the POJO classes. Here is a sample of the request payload (note the local name of the request payload):
</tns:placeOrderRequest>
<tns:order>
......
</tns:order>
</tns:placeOrderRequest>
The endpoint bean is registered using the endpoints
property. This property tells you that there should be a method in the endpoint
class (OrderServiceEndpoint
) with a name that starts with methodPrefix(handle)
and ends with the
request payload local name (placeOrderRequest)
. This increases the flexibility of the endpoint naming by using the configuration in spring-ws-servlet.xml
:
<bean class="org.springframework.ws.server.endpoint.mapping.SimpleMethodEndpointMapping"> <property name="endpoints"> <ref bean="OrderServiceEndpoint"/> </property> <property name="methodPrefix" value="handle"></property> <property name="interceptors"> <list> <bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"> <property name="logRequest" value="true" /> <property name="logResponse" value="true" /> </bean> </list> </property> </bean> <bean id="OrderServiceEndpoint" class="com.packtpub.liverestaurant.service.endpoint.OrderSeviceMethodEndpoint"> </bean>
The endpoint method name should match the handle+request
message root name (handleplaceOrderRequest
). In the body of the method, we should process the request and finally return the response in the form of javax.xml.transform.Source
:
public class OrderSeviceMethodEndpoint { private OrderService orderService; @Autowired public void setOrderService(OrderService orderService) { this.orderService = orderService; } public @ResponsePayload Source handleplaceOrderRequest(@RequestPayload Source source) throws Exception { //extract data from input parameter String fName="John"; String lName="Smith"; String refNumber="1234"; return new StringSource( "<tns:placeOrderResponse xmlns:tns=\"http://www.packtpub.com/liverestaurant/OrderService/schema\"><tns:refNumber>"+orderService.placeOrder(fName, lName, refNumber)+"</tns:refNumber></tns:placeOrderResponse>"); } }