Welcome to Java4u

A Single Place for all Java Resources

Looking for something?

Subscribe to this blog!

Receive the latest posts by email.

.Just enter your email below if you want to subscribe!

Email

Tuesday, June 17, 2014

JAX-RS

Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6 and higher, and make developers to develop REST web application easily.
JAX-RS provides the functionality for Representational State Transfer (RESTful) web services. REST is well suited for basic, ad hoc integration scenarios. RESTful web services, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL service–API definitions. 
Project Jersey is the production-ready reference implementation for the JAX-RS specification. Jersey implements support for the annotations defined in the JAX-RS specification, making it easy for developers to build RESTful web services with Java and the Java Virtual Machine (JVM).
In this series of JAX-RS tutorials, we use both Jersey and RESTEasy, popular JAX-RS implementation.
Because RESTful web services use existing well-known W3C and Internet Engineering Task Force (IETF) standards (HTTP, XML, URI, MIME) and have a lightweight infrastructure that allows services to be built with minimal tooling, developing RESTful web services is inexpensive and thus has a very low barrier for adoption. You can use a any development tool for developing Restful web services

Rest Full Web service API Design frame work



Annotations
@Path :
  1. It is used when we build a new class or when we use an method in a inner class.
  2. Responsible for routing the HTTP request to the proper method or class.
  3. This annotation can also be used with @PathParam annotation.
  4. Identifies the URL Path that java class will respond.
    1. E.g.: @Path (“/v1/message”)
    2. E.g.:@Path (“/v1/message/ {msg}”) (for @PathParam).
  5. Different Annotations @GET, @POST, @PUT, @DELETE, @HEAD
    1. @GET: mostly used to read and access the public methods.
    2. @POST: used to insert add / delete data or to submit data like login pages with HTTPS we can secure the data.
    3. @PUT: used for updating data mostly used for inserting/ adding the data.
    4. @DELETE: used delete the data.
    5. @HEAD: used to return meta-data for resource.
NOTE : we use only one for each method.
Mapping jersey annotations to the HTTP methods

@Produces :
  1. @Produces specify the media type that a method will produce and send back to the client.
  2. When you see @Produces at the class level, it treated as a default.
  3. When used with @Produces at  methods, it becomes a require to access that method.
  4. You can also define more than one for specific method.
  5. It does do some encoding but nothing extensive.
  6. you can define your own MediaType  as seen below
          E.g. : @Produces(MediaType.TEXT_HTML)
                            Or
                  @Produces(“text/html”)

If we need to send data to servers we have two ways to send it
  1. we can send it part of URL String by using URL Parameters
  2. we can send through the body of HTTP Request.
@Consumes :

  1. Annotation specifies where it is going to look for the data, and also what type of data it was.
  2. It is easy better to send data in the body, but if u use  something like get request than u can only use URL Parameters and we can also define our own consume types.
  3. For Eg.. with (pre-defined media types)
@Post
@Consumes(MediaType.APPLICATION_FORM_URLENCODE)
Public void saveMessage(String msg){
/*…..*/       //body
}

0 comments: