Programming RESTful Web Services in Java

3 minute read

Updated:

The Rest way of implementing the Web Service

  • image
  • Putting order on a part, and ur interested in list – then URL1 which is the list
  • U choose the part and access URL2 as a resource
  • If ur interested, you put a purchase order
  • Important here is you r in charge of sending HTTP get or post.
  • All three are resources

The SOAP way of implementing the Web Service

  • image
  • All the overhead here
  • Message, envelope, web server, opens it, invoke the method
  • Main diff is here the resources are used using SAME URL – so you need to specify the method you are gonna use
  • All three are methods

HTTP Methods

  • CRUD – create, read, update, delete
  • GET – defines a reading access of the resource without side-effects
  • PUT- creates a new resource
  • DELETE – removes the resources
  • POST - updates an existing resource of creates a new resource

Data Encoding and RPC

  • image
  • With SOAP, it is based on RPC – remote procedural call.
    • You make call to a remote object with necessary parameters, SOAP stack serialises parameters into XML and move the data using HTTP, receives response, deserializes it, and return
    • Data encoding expect to have both XML and JSON supported.
  • REST is very much on RPC side – you are accessing resource through HTTP protocol and that resource is identified as URI and you are still using a request reply protocol – to invoke a RESTful service.
  • They are still very close – RPC and REST
  • E.g.
    • Protobuf
      • Way of serialising structured data
      • Protobuf is in core of all internal RPC protocols in Google
    • Thrift
      • Software framework from Apache
      • Popular for services that needs cross-language service development - you develop sth in diff languages, and it supports a cross language services development
      • Comes with a code generation engine and allows ur software to write in various languages
    • Avro - Data serialisation system
    • GSON
      • Google – Google, de/serialisation library just like JSON
      • Converts Java objects into JSON and back.

Java and REST

  • Once I know how to write a program – we can say REST is server and client
  • There’s java specification request 311 called JAX-RS
  • Itself uses annotations to define the REST relevance of Java classes
  • Idea is to use Class with REST style
  • Together with. Java Architecture for XML Binding – JAXB
  • We can take the specification and implement it
    • Make sure it’s interoperable.
    • One of them is Jersey

Jersey

  • Reference implementation for JAX-RS
  • Contains a RESR server and a RESR client
  • Server side – uses servlet which scans predefined classes to identify RESTful resources
  • More importantly how jersey expects you to use concept of resources
  • http://your_domain:port/display-name/url-pattern/path_from_rest_class
    • ur domain
    • local host
    • display name
    • url pattern
    • path from the class
  • there are other things as well – RESTEasy, Struts, etc

JAX-RS Annotations

  • how do I know how to annotate the code?
  • HTTP relies on Post, get, put, delete – I need understanding and how I’m gonna annotate them
  • 7 annotations are enough to deploy a web service
  • @PATH(your path) - Before all this I need to set my base url
  • image
  • @Produces(MediaType) – I define what media type is delivered by a method @GET. i.e. xml? Html? json? Plaintext?
  • @Consumes – it defines media type that is consumed by the method – which is usually a post.

Steps

  1. Create the Restful Web Service
    1. Looks like I’m gonna set the path to the base URL
    2. Once I do that I create a class CalcREST and think of parameters
    3. Add and expect 2 parameters
    4. Id like to produce a media type – as a plain text
    5. A method that is attached to addd get – this would expect 2 parameters
    6. And the method returns a+b
    7. Second get – same thing to be done but the media type is XML!!
  2. Publish the Restful Web Service
    1. We need to know where to access it – what’s the URL – the fact that I attached URL is imp
    2. I’m gonna start the server
  3. Create a Restful Web Service Client
    1. Client can do add and sub
    2. Gonna create a class and create client through that
    3. Gonna create addService – which adds 2 and get the response
    4. Through XML or Text

Testing the Service

  • How do I write my client to use this service
  • With jersey, you can see whether ur service is up and running through http://localhost:9999/calcrest/application.wadl

Leave a comment