REST Web Services with Java Part 3 - vulabvulab

Posted on 06 March 2017 by Srinivas Nelakuditi

REST Web Services with Java Part 3

Let us look at a sample web service code snippet created using JAX-RS annotations

package com.vulab.code;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;

// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {

     // The Java method will process HTTP GET requests
     @GET
     @Produces("text/plain")
     public String helloWorldVulab() {
          return "Hello World Vulab";
      }
}


In the above code snippet. @Path annotation is having the value of /helloworld

If a client wants to call the method helloWorldVulab, They can just use this URL in the web browser.

http://example.com/helloworld

Because you are automatically making a HTTP GET request. You call above will match with the @PATH annotation value and as you are making a HTTP GET request, it matches with @GET annotation on method helloWorldVulab. The method executes and you can see ‘Hello World Vulab’ on the web browser. @Produces annotation will allow the method to send the text as plain text. You can even send JSON or XML back to the caller with the @Produces annotation.

0 Comments

Leave a Reply