Restful Web Service


Published on

a presentation for COMP6017 coursework

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Good afternoon ladies and gentlemen. We appreciate the opportunity to be with you. This is Cai bin, the leader of our group, this is Ma ning and I am Gao yuan. The topic of our group is REST.\n
  • Basically, we have divided our presentation into six parts. We will start by an introduction of the project and the technologies and toolkits which should be used in our project. Next, we will discuss its implementation, security, deploy and test. And to finish off, we will analyse and evaluate our project.\n
  • REST is short for Representational State Transfer, which is a way of design and development for web applications. It is different from soap style services. RESTful web services use HTTP protocol to send and receive messages. In the rest, all the things are abstracted as resources and each resource has a unique URI.\n
  • Our project consists of one client, three services and some resources. The client of our project is used ajax and the three services are travel agent, flight and hotel. We define different URLs to mark different resources such as flight, hotel, booking and query.\n
  • Now, let’s move on to the next part of our presentation: the technologies and toolkits. We apply three main technologies to realize our project: ajax and xml and jax-rs. Ajax is used to send request and receive reponse and xml is used to store and transmit data. Jax-rs is a java application for restful web services.\n
  • We also use three toolkits to complete our project: Maven, Jersey and Grizzly. Maven is a project management which is to manage the whole project. Jersey is a jax-rs reference implementation for building restful web services which provides API as an interface. Grizzly is a web application container.\n
  • This is the architecture diagram of our project. We have already explained it before. So we skip this slide.\n
  • Hello, everyone. I will step into the implementation part of our presentation now.  Because we are going to implement three services which are travelagent, flight and hotel, we define several classes at first. For example, we use flgiht class to define a flight and hotel class to define hotel.  As you can see from the code, we use XMLRootElement annotation to declare the data format of XML and we also add some attributes to the entities. Other classes, such as TravelAgent, FlightResource, HotelResource, are set up to execute requested command and return response.\n
  • We use Jersey as our toolkit. Because it provide JAX-RS implementation APIs which are easy to use. The annotations which can be used as interface. The Path annotation will define a url path where the resource is located. GET and POST annotation will receive HTTP get or post request and execute the code blew. Produces annotation define the  returned data format. There are also other annotations that can be used. Now let’s look at some pieces of code as example.\n
  • This is some code in FlightResource class. We use @path(“/flight”) to define the flight resource as slash flight and @Produces("application/xml") to declare the returned data format is XML. We use TreeMap to store the flight information. When the service receive a get request, the code blew @GET will execute and return the all flights.\n
  • When the service receive a get request with a specific flightNo, this code will execute and return the requested flight information. The PathParam annotation will get the value of flightNo and pass to cId. Other annotations include QueryParam @Context and so on.\n
  • Security is really a problem in restful web services, as WS-security is not suitable for rest. Fortunately, we can use HTTPS to enhance the basic http authentication. HTTPS can ensure the request form and return message to the right customer. We use class HTTPSProperties to set up these two packages. For authentication, we use @Context annotation to receive user identity and use these code to verify it.\n
  • what we create is a web application which will be hosted on a server. We use grizzly as the web container. This is the code in web.xml which config the deployment of our application. The red lines define the parameters which will deploy the package to the servlet\n
  • For test, we used Jersey client API to set up our test cases. You can see from the code, which is a sample of test case. In this test, the client will send a get request to the travelagent which  will return options of available flights and hotels.\n
  • Hi, everyone! I will finish this presentation with the last part, which is the analysis and evaluation of our project. Before we start this coursework, we have little experience of neither web service nor rest style. So we spend some time to understand the conception of rest architecture. This is a fundamental process of studying by ourselves. Until now I think we have already had a good understanding of restful web services. After we started, we compared some toolkits such as CXF, Axis2 and so on, while we chose jersey as the source community finally. Because Jersey provides complete Java APIs for restful web services which are easy to use and implement.\n\nWe also came across some challenges. When we were going to implement security in our project, we found it is difficult to use WS-security in restful web service and other methods maybe not suitable for our scenario and Jersey is lack of the security APIs to use. So we use transport layer security which is HTTPs instead of application layer security. For test, we cannot write regular unit test case and we had to choose using jersey client to test our project.\n\nThe last point is that we always work as a team. We discuss and think about the way to implement our project together and share the gains with others. As a member of this group, I developed the travel agent service, Bin developed flight service and Test function, and Yuan implement hotel service and security. We did really hard work to complete our work. So I hope everyone can enjoy our project and this presentation.\nThank you!\nAny question?\n
  • Thank you!\nAny questions?\n
  • Restful Web Service

    1. 1. Group15 Rest Yuan Gao Ning Ma Bin Cai
    2. 2. OverviewIntroductionTechnologies and toolkitsArchitectureImplementation & SecurityDeploy & TestAnalysis and evaluation
    3. 3. IntroductionRESTful Web Services is different from SOAP-style servicesusing HTTP GET PUT POST DELETEUnique URI to describe Resource
    4. 4. IntroductionClient3 Services:TravelAgent, Flight, HotelResources:/flight/hotel/booking/query
    5. 5. Technologies and ToolkitsAJAX: send request and receive responseXML: store and transmit dataJAX-RS (JSR 311): Java API for RESTful WebServices
    6. 6. Technologies and ToolkitsMaven: project managementJersey: JAX-RS Reference Implementation forbuilding RESTful Web servicesGrizzly: Web application container
    7. 7. Architecture Diagram
    8. 8. Implementation define classes:import importjavax.xml.bind.annotation.XmlRootElement; javax.xml.bind.annotation.XmlRootElement;@XmlRootElement @XmlRootElementpublic class Flight { public class Hotel {! ! private int Id;! private int flightNo; ! private String Name;! private String from; ! private int price;! private String to; ! private String address;! private int departTime; ! public Hotel() {}! private int arriveTime; ...! private int price;! public Flight() {}... TravelAgent, FlightResource, HotelResource...etc...
    9. 9. ImplementationJersey API annotation:@Path@GET@POST@Produces...Let’s look at some pieces of code as example:
    10. 10. Some code...@Path("/flight")@Produces("application/xml")public class FlightResource {!! public TreeMap<Integer, Flight> flightMap = new TreeMap<Integer, Flight>(); ...! @GET! public List<Flight> getFlights() {! List<Flight> flights = new ArrayList<Flight>();! flights.addAll(getFlightMap().values());! return flights;! }
    11. 11. Some code...... @GET! @Path("{flightNo}")! public Flight getFlight(@PathParam("flightNo") int cId) {! return getFlightMap().get(cId);! }...
    12. 12. SecurityHTTPS: use class HTTPSProperties to SecurityContext customerif (customer.isUserInRole("user") {   return new Flight();   } else {   return “please provide effective customer”;   }
    13. 13. Deploy<web-app> <servlet> <servlet-name>Jersey Web Application</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name></param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey Web Application</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping></web-app>
    14. 14. Test use Jersey client api to set up test casetry { ! ! System.out.println("==> Test 1: Get all flight "); ! ! ! ClientConfig config = new DefaultClientConfig(); ! ! Client client = Client.create(config); ! ! client.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true); ! ! WebResource service = client.resource("http://localhost:8080/");System.out.println(service.path("rest2011").path("travelagent").type(MediaType.APPLICATION_FORM_URLENCODED).get(String.class)); System.out.println("n"); ! }...
    15. 15. Analysis and evaluationUnderstanding REST architectureUse toolkits to implement scenarioChallengesWork as a team
    16. 16. Thank you!
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.