Using Java to implement RESTful Web Services: JAX-RS
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Using Java to implement RESTful Web Services: JAX-RS

  • 5,915 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,915
On Slideshare
5,915
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
64
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • helloworld
  • The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client. If @Consumes is applied at the class level, all the response methods accept the specified MIME types by default. If applied at the method level, @Consumes overrides any @Consumes annotations applied at the class level.If a resource is unable to consume the MIME type of a client request, the JAX-RS runtime sends back an HTTP 415 ( “ Unsupported Media Type ” ) error.The value of @Consumes is an array of String of acceptable MIME types. For example:@Consumes({"text/plain,text/html"})The following example shows how to apply @Consumes at both the class and method levels:@Path("/myResource")@Consumes("multipart/related")public class SomeResource { @POST public String doPost(MimeMultipart mimeMultipartData) { ... } @POST @Consumes("application/x-www-form-urlencoded") public String doPost2(FormURLEncodedProperties formData) { ... }}The doPost method defaults to the MIME media type of the @Consumes annotation at the class level. The doPost2 method overrides the class level @Consumes annotation to specify that it can accept URL-encoded form data.If no resource methods can respond to the requested MIME type, an HTTP 415 ( “ Unsupported Media Type ” ) error is returned to the client.The HelloWorld example discussed previously in this section can be modified to set the message by using @Consumes, as shown in the following code example:@POST@Consumes("text/plain")public void postClichedMessage(String message) { // Store the message}In this example, the Java method will consume representations identified by the MIME media type text/plain. Note that the resource method returns void. This means that no representation is returned and that a response with a status code of HTTP 204 ( “ No Content ” ) will be returned.

Transcript

  • 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) and resources 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 requests @GET // The Java method will produce content identified by the MIME Media // type "text/plain" @Produces("text/plain") public String getClichedMessage() { // Return some cliched textual content return "Hello World"; }}
  • 5. @Path Annotation and URI Path Templates@Path 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 @PathParam annotation may be used on the method parameter of a request method@Path("/users/{username}")public class UserResource { @GET @Produces("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 Annotation@Produces 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 @Produces annotations applied at the class level
  • 9. @produces annotation@Path("/myResource")@Produces("text/plain")public class SomeResource { @GET public String doGetAsPlainText() { ... } @GET @Produces("text/html") public String doGetAsHtml() { ... }}
  • 10. @Consumes Annotation@Consumes 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 Netbeans 03/28/11
  • 12. Develop Restful service in Netbeans 03/28/11
  • 13. Develop Restful service in Netbeans 03/28/11
  • 14. Develop Restful service in Netbeanspackage orders;import javax.ws.rs.*;@Path("order")public class OrderResource { public OrderResource() { } @GET @Produces("application/xml") public Order getLastOrder() { return Orders.getLastOrder(); } @PUT @Consumes("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 Netbeans 03/28/11
  • 17. Test Restful service in Netbeans 03/28/11
  • 18. 03/28/11
  • 19. Testing Restful servicesNetbeans test client does not work well with XML parametersBetter 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 specify this in the header 03/28/11
  • 22. Add header parameters 03/28/11
  • 23. Add header parameters 03/28/11
  • 24. Placing an order 03/28/11
  • 25. k.verbert@tue.nln.v.stash@tue.nlg.l.fletcher@tue.nl 03/28/11