JAX-RS @Path Regex based URI matching Example

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;
 
    }
}

 

No comments:

Post a Comment

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts