1 September 2018

RD EE6 Web Services module 21 lab using JAX-RS 2

Create a JAX-RS 2 REST client application for your createAuthor and findAuthorById service.

  1. Make a REST client prject
    1. Make a new rsClient java application project  in Netbeans
    2. Make sure your Author webservice server project is open as well
    3. Add a new webservice client to the client project (File > new > Webservices > RESTful Java client)
      1. Select the REST resource, browse for your server project and select the Author REST service 
      2. Finish
        1. This creates you a project with the JAX-RS libraries and client sample code.
          1. You can use the sample code for your inspiration, but write your own client code
    4. Copy the domain classes of your server project + the JAXB adapters to the client project
  2. Call the createAuthor service
    1. In the main method of your client project create a javax.ws.rs.client.Client object
    2. Create a request that expects an XML or JSON reply
    3. Create a new Author object, without an id
    4. POST the author object in the body as a XML or JSON Entity. Example of the creation of a JSON entity: javax.ws.rs.client.Entity.json(author)
  3. Call the findAuthorById service to retrieve the Author you just created from the server
    1. If the createAuthor service is well written, the Location header in the javax.ws.rs.core.Response should contain the URL of the newly created Author (including the generated author id). Retrieve the URL from the Location header in the Response.
    2. Use the URL to retrieve the author, converted by the Client to an Author object
    3. Print the Author object to the console

No comments:

Post a Comment