Programming RESTful Web Services in Java
Updated:
- The Rest way of implementing the Web Service
- The SOAP way of implementing the Web Service
- Data Encoding and RPC
- Steps
The Rest way of implementing the Web Service
- 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
- 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
- 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.
- Protobuf
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
- @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
- Create the Restful Web Service
- Looks like I’m gonna set the path to the base URL
- Once I do that I create a class CalcREST and think of parameters
- Add and expect 2 parameters
- Id like to produce a media type – as a plain text
- A method that is attached to addd get – this would expect 2 parameters
- And the method returns a+b
- Second get – same thing to be done but the media type is XML!!
- Publish the Restful Web Service
- We need to know where to access it – what’s the URL – the fact that I attached URL is imp
- I’m gonna start the server
- Create a Restful Web Service Client
- Client can do add and sub
- Gonna create a class and create client through that
- Gonna create addService – which adds 2 and get the response
- 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