6. The client (i.e. html file) that we will create will send flight reservation details in the request body as a JSON string. This JSON string has to be parsed and mapped to FlightReservationDetails object. Similarly the FlightReservationDetails object has to be converted to JSON in the response body. The mapping from JSON to Java object and vice versa will be done using MappingJacksonHttpMessageConverter and this has to be configured in spring-ws-servlet.xml. Also enable component-scan to scan com.flight package.
<?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:sws="http://www.springframework.org/schema/web-services"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<sws:annotation-driven/>
<context:component-scan base-package="com.flight" />
<bean id="jsonConverter"
class="org.springframework.http.converter.json
.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation
.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
</beans>
|
7. In pom.xml, add the Jackson mapper dependency
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
|
8. In web.xml use DispatcherServlet instead of MessageDispatcherServlet. This is because DispatcherServlet is used to handle HTTP requests and response whereas MessageDispatcherServlet handles SOAP web service messages. Also add load-on-startup tag and change the URL pattern to /rest/*
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" version="2.4">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
|
Creating a Test Client for testing our RESTFul web service
In this step we will create an html file and use jquery-1.7.2.min.js and make rest API calls to create a reservation, retrieve reservation, update reservation and delete reservation.
Create html file under WEB-INF directory. Download jquery-1.7.2.min.js and copy it in WEB-INF directory.
<html>
<head>
<title>Restful Web Service</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
</head>
<body>
<input id="CreateReservation" type="button" value="Create Reservation" />
<br>
<input id="GetReservation" type="button" value="Get Reservation" />
<br>
<input id="UpdateReservation" type="button" value="Update Reservation" />
<br>
<input id="DeleteReservation" type="button" value="Delete Reservation" />
</body>
<script>
$(document).ready(function() {
$("#CreateReservation").click(function() {
createReservation();
});
$("#GetReservation").click(function() {
getReservation();
});
$("#UpdateReservation").click(function() {
updateReservation();
});
$("#DeleteReservation").click(function() {
deleteReservation();
});
function createReservation() {
var restCreateAPI = "http://localhost:8080/flight-rest-ws/rest/reservation/user123/create";
var postdata = "{\"flightNumber\":\"FLIGHT-123\", \"source\":\"CITY-A\", \"destination\":\"CITY-B\",\"date\":\"JAN 1 2014\",\"time\":\"09:00 AM\"}";
$.ajax({
url : restCreateAPI,
type : 'POST',
data : postdata,
contentType : "application/json; charset=utf-8",
dataType : 'json',
async : true,
success : function(response) {
alert(response.message + " Flight Reservation Number is " + response.reservationNumber);
},
error : function(responseData) {
}
});
}
function getReservation() {
var restGetAPI = "http://localhost:8080/flight-rest-ws/rest/reservation/user123/get/RID123456";
$.ajax({
url : restGetAPI,
type : 'GET',
dataType : 'json',
async : true,
success : function(response) {
var reservationDetails = "Flight Number: " + response.flightNumber +
", source: " + response.source +
", destination: " + response.destination +
", date: " + response.date +
", time: " + response.time;
alert(reservationDetails);
},
error : function(responseData) {
}
});
}
function updateReservation() {
var restUpdateAPI = "http://localhost:8080/flight-rest-ws/rest/reservation/user123/update/RID123456";
var postdata = "{\"flightNumber\":\"FLIGHT-123\", \"source\":\"CITY-A\", \"destination\":\"CITY-B\",\"date\":\"JAN 5 2014\",\"time\":\"11:00 AM\"}";
$.ajax({
url : restUpdateAPI,
type : 'PUT',
data : postdata,
contentType : "application/json; charset=utf-8",
dataType : 'json',
async : true,
success : function(response) {
alert(response.message + " Flight Reservation Number is " + response.reservationNumber);
},
error : function(responseData) {
}
});
}
function deleteReservation() {
var restDeleteAPI = "http://localhost:8080/flight-rest-ws/rest/reservation/user123/delete/RID123456";
$.ajax({
url : restDeleteAPI,
type : 'DELETE',
dataType : 'json',
async : true,
success : function(response) {
alert(response.message);
},
error : function(responseData) {
}
});
}
});
</script>
</html>
|
No comments :
Post a Comment