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 :
- It is used when we build a new class or when we use an method in a inner class.
- Responsible for routing the HTTP request to the proper method or class.
- This annotation can also be used with @PathParam annotation.
- Identifies the URL Path that java class will respond.
- E.g.: @Path (“/v1/message”)
- E.g.:@Path (“/v1/message/ {msg}”) (for @PathParam).
- Different Annotations @GET, @POST, @PUT, @DELETE, @HEAD
- @GET: mostly used to read and access the public methods.
- @POST: used to insert add / delete data or to submit data like login pages with HTTPS we can secure the data.
- @PUT: used for updating data mostly used for inserting/ adding the data.
- @DELETE: used delete the data.
- @HEAD: used to return meta-data for resource.
NOTE : we use only one for each method.
Mapping jersey annotations to the HTTP methods
@Produces :
- @Produces specify the media type that a method will produce and send back to the client.
- When you see @Produces at the class level, it treated as a default.
- When used with @Produces at methods, it becomes a require to access that method.
- You can also define more than one for specific method.
- It does do some encoding but nothing extensive.
- you can define your own MediaType as seen below
Or
@Produces(“text/html”)
If we need to send data to servers we have two ways to send it
- we can send it part of URL String by using URL Parameters
- we can send through the body of HTTP Request.
- Annotation specifies where it is going to look for the data, and also what type of data it was.
- 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.
- For Eg.. with (pre-defined media types)
@Post
@Consumes(MediaType.APPLICATION_FORM_URLENCODE)
Public void saveMessage(String msg){
/*…..*/ //body
}
0 comments:
Post a Comment