Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Let's look at an example using the JAX-RS @Get, @Path, and @PathParam annotations (example is from https://www.javatpoint.com/jax-rs-annotations-example)

@Get annotation

Code Block
languagejava
titleHelloService.java
linenumberstrue
package com.javatpoint.rest;  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.core.Response;  

@Path("/hello")  
public class HelloService
{  
    @GET  
    public Response getMsg() 
    {  
        String output = "Jersey says hello world;  
        return Response.status(200).entity(output).build();  
    }  
}  

...

ReST is an architecural style that is dependant on the open HTTP protocol.  Web Services which are direct implementation of that architectural style will only use HTTP.  Hence the @Get, @Post, and @Put annotations. Marking a method with any of these HTTP commands indicates to the servlet engine to only route messages to that method if it can handle that command.  So in the above example the getMsg() method will be invoked on a GET command for messages to <host>/rest/hello.  

@Path annotation

Code Block
languagejava
titleHelloService.java
linenumberstrue
package com.javatpoint.rest;  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.core.Response;  

@Path("/hello")  
public class HelloService
{  
    @GET  
    @Path("/help")  
    public Response getHelp() 
    {  
        String output = "Jersey is getting you some help ";  
        return Response.status(200).entity(output).build();  
    }  
}  

When the @Path annotation is use to route messages to specific methods depending on the sub-url that is typed.  In the example above messages to <host>/hello/help will cause the servlet engine to invokce the method getHelp().  This is a useful feature for routing based on variations in the sub-urls.

@Path and @PathParam annotations

Code Block
languagejava
titleHelloService.java
linenumberstrue
package com.javatpoint.rest;  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.core.Response;  

@Path("/hello")  
public class HelloService
{  
    @GET  
    @Path("/{param}")  
    public Response getParameterisedMsg(@PathParam("param") String msg) 
    {  
        String output = "Jersey say : " + msg;  
        return Response.status(200).entity(output).build();  
    }  
}  

The Web Services standard also allows parameters to be passed as part of the url.  It is done by appending the parameters to the base url as though they are a sub-url.  In the above example if you enter the base url <host>/hello/help me, the servlet engine will route the message to the method getParameterisedMsg().  The @Path on the method contains /{param} which is a indicator to the engine to interpret the value after the base url <host>/hello as a parameter.  The paramater can then be injected into the method using the @PathParam annotation, the value in the quotes must match the value in the @Path annotation on the method and between the braces.


Working with multiple @PathParam annotations

Code Block
languagejava
titleHelloService.java
linenumberstrue
package com.javatpoint.rest;  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.core.Response;  

@Path("/hello")  
public class HelloService
{  
    @GET  
    @Path("{year}/{month}/{day}")  
    public Response respondWithDOB(  
            @PathParam("year") int year,  
            @PathParam("month") int month,   
            @PathParam("day") int day) {  
   
       String date = year + "/" + month + "/" + day;  
   
       return Response.status(200)  
        .entity("getDate is called, year/month/day : " + date)  
        .build();  
    }    
}  

...