Searching...
Friday, 19 July 2013

JAX-RS @Path Regex based URI matching Example

13:30

Restful Web service

Representational State Transfer

REST is an architectural style which is based on web-standards and the HTTP protocol.

see more details in Wikipedia

HTTP Methods

The PUT, GET, POST and DELETE methods are typical used in REST based architectures.

see more details in Wikipedia

Java Restful Implementations

See more details Wikipedia

JAX-RS @Path Regex based URI

In complex form, we can use regular expression based parameter matching.

e.g. @Path("/{param: [0-9]+}")

In this case the web service method will accept only the number parameter, see the below example

Example

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
 
@Path("/statepatternparam")
public class PathPatternExample {
   @GET
    @Produces("application/xml")
    @Path("/{param: [0-9]+}")
    public String getMsg(@PathParam("param") String state) {
 
        String stateDetails = null;
 
        if (state.equals("13")) {
 
            stateDetails = "<State><name>KERALA</name><shortname>KL</shortname>"
                    +
 
                    "<headq>TRIVANDRUM</headq><language>MALAYALAM</language></State>";
 
        } else {
 
            stateDetails = "Data not found";
 
        }
 
        return stateDetails;
 
    }
}

 

0 comments: