Introduction to Restful web service – A JAX-RS specification

In this article, we will learn and understand about web service and its different types. Further, we will concentrate on the Restful web service

Web Service

An inter-operable service hosted & up-running over the web which could be accessed by different independent clients like web browser

Types of Web Services

  • REST (Representational State Transfer)
  • SOAP (Simple Object Access Protocol)

 

REST

  • JAX-RS 2.0 specification supports implementation of Restful web services
  • Different vendor implementation for this type are
    1. Apache CXF
    2. JBoss RestEasy
    3. Oracle Jersey (Prior 2.x)
    4. Glassfish Jersey (2.x)

 

SOAP

  • JAX-WS 2.0 specification supports implementation of SOAP based web services
  • Different vendor implementation for this type are
    1. JAX-WS viz., Top-down and Bottom-up approach
    2. Apache Axis
    3. Apache Axis2
    4. Glassfish Metro JAX-WS
    5. Apache CXF
    6. Oracle WebLogic JAX-WS
    7. IBM WebSphere JAX-WS
    8. JBoss WS

Introduction_To_Web_Services

Source: BenchResources.net team

 

In this particular article, we will discuss and learn JAX-RS 2.0 specification based Restful web service. Later in the upcoming articles, we will use Apache’s CXF – Celtix + XFire to implement demo examples

Restful Web Services

In the REST of World, you call everything as resource and it is uniquely identified using URI i.e.; Uniform Resource Identifier. REST server and clients interacts using stateless HTTP protocol via four broadly classified HTTP verbs viz.,

  • POST            – to CREATE/insert resource
  • GET              – to READ/select/retrieve internal resource
  • PUT              – to UPDATE/modify the resource
  • DELETE         – to DELETE/remove internal resource representation

Note: That forms CRUD

Relationships between SQL and HTTP Verbs

 Action  SQL  HTTP verbs
Create Insert POST
Read Select GET
Update Update PUT
Delete Delete DELETE

Formats supported in Restful Web Services

  • XML
  • JSON
  • TEXT
  • Image
  • HTML
  • PDF
  • etc

Advantages of Rest web service

  • REST architectural pattern is basically lightweight in nature. So, when you have bandwidth constraints then prefer REST web service
  • Easy and fast to develop
  • Top sites like Twitter, Yahoo uses this pattern
  • Most social networking sites like facebook.com uses REST web services
  • Mobile App development growing rapidly and for their server interaction, it uses this REST pattern as it is faster in processing request/response data

JAX-RS Annotations

These are the most commonly used JAX-RS Annotations, when developing REST based web service. In the upcoming articles, we will understand & use these annotations and implement demo examples

  • @Path (javax.ws.rs.Path)
  • @GET (javax.ws.rs.GET)
  • @POST (javax.ws.rs.POST)
  • @PUT (javax.ws.rs.PUT)
  • @DELETE (javax.ws.rs.DELETE)
  • @PathParam (javax.ws.rs.PathParam)
  • @QueryParam (javax.ws.rs.QueryParam)
  • @MatrixParam (javax.ws.rs.MatrixParam)
  • @FormParam (javax.ws.rs.FormParam)
  • @HeaderParam (javax.ws.rs.HeaderParam)
  • @Context (javax.ws.rs.core.Context)
  • @Consumes (javax.ws.rs.Consumes)
  • @Produces (javax.ws.rs.Produces)

@Path annotation (explanation)

  • @Path specifies the relative URI path
  • It can be defined at class-level or method-level
  • Let understand by an example

1. Base URL for any web application is

http://<server>:<port>/<context-root>

2. Append <url-pattern> tag from web.xml with Base URL that forms

http://<server>:<port>/<context-root>/<url-pattern>

3. Append class-level @Path(“class-level”) annotation and then method-level @Path(“method-level”) annotation that forms

http://<server>:<port>/<context-root>/<url-apptern>/<class-level>/<method-level>

Go through the examples in the upcoming articles to understand other annotations in details

 

Read about SoapUI tutorials from guru99.com

 

References

Apache CXF: http://cxf.apache.org/docs/jax-rs.html
JBoss RestEasy: http://docs.jboss.org/resteasy/docs/3.0.7.Final/userguide/html/
Jersey: https://jersey.java.net/download.html
Rest: http://rest.elkstein.org/
Redhat: https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/Developing_RESTful_Web_Services/files/RESTIntro.html

 

Happy Coding !!
Happy Learning !!

Apache CXF: JAX-RS Restful web service using @PathParam annotation