Click here to load reader

Using Java to implement RESTful Web Services: JAX-RS

  • View
    5.770

  • Download
    1

Embed Size (px)

Text of Using Java to implement RESTful Web Services: JAX-RS

  • 1.Using Java to implement REST WebServices: JAX-RSWeb Technology2II25Dr. Katrien VerbertDr. ir. Natasha StashDr. George Fletcher

2. Restful Web Services Frameworks and APIs JAX-RS - The Java API for RESTful Web Services uses annotations to make plain old Java objects (POJOs) andresources available through HTTP Sun Reference Project: Jersey Other Vendors: CXF (Apache), RESTEasy(JBoss) and Restlet JAX-RS tutorial: http://docs.oracle.com/javaee/6/tutorial/doc/gilik.html 3. JAX-RS HTTP Templated mapping and subresources @Path MIME handling @Provides, @Consumes HTTP methods @GET, @POST, @UPDATE, @DELETE, @HEAD, @OPTIONS, @HttpMethod Caching evaluatePreconditions 4. Examplepackage com.sun.jersey.samples.helloworld.resources;import javax.ws.rs.GET;import javax.ws.rs.Produces;import javax.ws.rs.Path;// The Java class will be hosted at the URI path "/helloworld"@Path("/helloworld")public class HelloWorldResource {// The Java method will process HTTP GET [email protected]// The Java method will produce content identified by the MIME Media// type "text/plain"@Produces("text/plain")public String getClichedMessage() {// Return some cliched textual contentreturn "Hello World";}} 5. @Path Annotation and URI Path [email protected] annotation identifies the URI path template to which the resource responds is specified at the class or method level of a resourceURI path templates are URIs with variables embedded within the URI syntax these variables are substituted at runtime variables are denoted by braces ({ and }) @Path("/users/{username}") example request http://example.com/users/Galileo 6. @PathParam annotationTo obtain the value of the user name, the @PathParamannotation may be used on the method parameter of arequest [email protected]("/users/{username}")public class UserResource {@[email protected]("text/xml")public String getUser(@PathParam("username") String userName) {...}} 7. Examples of URI Path Templates URI Path Template URI After Substitution http://example.com/{name1}/{name2}/ http://example.com/james/gatz/ http://example.com/{question}/http://example.com/why/why/why/ {question}/{question}/ http://example.com/maps/{location}http://example.com/maps/Main%20Street 8. @Produces [email protected] annotation is used to specify the MIME media types or representations a resource can produce and send back to the client applied at the class level: default for all methods applied at the method level overrides any @Producesannotations applied at the class level 9. @produces [email protected]("/myResource")@Produces("text/plain")public class SomeResource {@GETpublic String doGetAsPlainText() {...}@[email protected]("text/html")public String doGetAsHtml() {...}} 10. @Consumes [email protected] annotation is used to specify which MIME media types of representations a resource can accept @POST @Consumes("text/plain") public void postClichedMessage(String message) { // Store the message } 11. Develop Restful service in Netbeans03/28/11 12. Develop Restful service in Netbeans03/28/11 13. Develop Restful service in Netbeans03/28/11 14. Develop Restful service in Netbeanspackage orders;import javax.ws.rs.*;@Path("order")public class OrderResource {public OrderResource() {}@[email protected]("application/xml")public Order getLastOrder() {return Orders.getLastOrder();}@[email protected]("application/xml")public void putOrder(Order order) {Orders.setLastOrder(order);}} 15. Develop Restful service in Netbeanspackage orders;public class Orders {private static Order lastOrder=new Order();public static Order getLastOrder() {return lastOrder;}public static void setLastOrder(Order lastOrder) {Orders.lastOrder = lastOrder;}} 16. Deploy Restful service in Netbeans03/28/11 17. Test Restful service in Netbeans03/28/11 18. 03/28/11 19. Testing Restful servicesNetbeans test client does not work well with XMLparametersBetter alternative:RESTClient, a debugger for RESTful web serviceshttps://addons.mozilla.org/nl/firefox/addon/restclient/03/28/11 20. RESTClient 03/28/11 21. Testing Restful servicesIf you pass XML strings as parameters, you need to specifythis in the header03/28/11 22. Add header parameters03/28/11 23. Add header parameters03/28/11 24. Placing an order 03/28/11 25. [email protected]@[email protected] 03/28/11

Search related