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

Jersey 1.x Implementation

Java Rest Full Web service with jersey frame work
Jersey frame work
  • It’s like a gate keeper.
  • And its loads jersey frame work into our application
  • It handles by creating the listener and all types of HTTP Request and helps how to route this HTTP requests.
  • And also Error Handling
  • Easy To use and Its Well Defined
And see there is any java class is there to handle the request.So  we do have one /rest/v1/message in our class the api will handle that request and the method and  next the method will executed.Once its get executed successfully,it will rerun data back to the browser.



This servlet analyzes the incoming HTTP request and selects the correct class and method to respond to this request. This selection is based on annotations in the class and methods.

The class registers its methods for the HTTP GET request using the @GET annotation.
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

  @Path("/v1/message")//route the java class
   @Produces(“text/html”)
    public class VersionMessage {
   private static final String version = "1.0.0"// version to the api

   @GET
   //by default this method will be executed
   public String getReturnMessage() {
          return "Welcome  To RestFul WebSerivce";
   }

   @Path("/version"// route to the specfied method
   @GET
   @Produces(“text/html”)
   public String getVersionMessage() {
           return "RestFul WebSerivce : v"+version+"";
   }
}

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.
@Post
@Consumes(MediaType.APPLICATION_FORM_URLENCODE)
Public void saveMessage(String msg){
/*…..*/       //body
}

0 comments: