JAX-RS @Path URI matching example

In JAX-RS, you can use @Path to bind URI pattern to a Java method. See following examples to show you how it works.

1. Normal URI Matching

See normal URI matching with @Path annotation.


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserRestService {

	@GET
	public Response getUser() {

		return Response.status(200).entity("getUser is called").build();

	}

	@GET
	@Path("/vip")
	public Response getUserVIP() {

		return Response.status(200).entity("getUserVIP is called").build();

	}
}

URI pattern : “/users


getUser is called

URI pattern : “/users/vip


getUserVIP is called

2. URI Matching and Parameter

The value within an open brace “{” and close brace “}”, is represents a parameter, and can be access with @PathParam.


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserRestService {

	@GET
	@Path("{name}")
	public Response getUserByName(@PathParam("name") String name) {

		return Response.status(200)
			.entity("getUserByName is called, name : " + name).build();

	}

}

URI Pattern : “/users/mkyong


getUserByName is called, name : mkyong

URI Pattern : “/users/abcdefg


getUserByName is called, name : abcdefg

3. URI Matching and Regular Expression

@Path support complex URI matching with regular expression, via following expression :


{" variable-name [ ":" regular-expression ] "} 

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserRestService {

	@GET
	@Path("{id : \\d+}") //support digit only
	public Response getUserById(@PathParam("id") String id) {

	   return Response.status(200).entity("getUserById is called, id : " + id).build();

	}

	@GET
	@Path("/username/{username : [a-zA-Z][a-zA-Z_0-9]}")
	public Response getUserByUserName(@PathParam("username") String username) {

	   return Response.status(200)
		.entity("getUserByUserName is called, username : " + username).build();

	}
	
	@GET
	@Path("/books/{isbn : \\d+}")
	public Response getUserBookByISBN(@PathParam("isbn") String isbn) {

	   return Response.status(200)
		.entity("getUserBookByISBN is called, isbn : " + isbn).build();

	}

}

URI Pattern : “/users/999


getUserById is called, id : 999

URI Pattern : “/users/123456


getUserById is called, id : 123456

URI Pattern : “/users/username/aaa” , failed, don’t match “[a-zA-Z][a-zA-Z_0-9]”, first character need “[a-zA-Z]”, second character need “[a-zA-Z_0-9]”.


Could not find resource for relative : /users/username/aaa

URI Pattern : “/users/username/a9


getUserByUserName is called, username : a9

URI Pattern : “users/books/999


getUserBookByISBN is called, isbn : 999

Download Source Code

Download it – JAX-RS-Path-URI-Matching-Example.zip (6 KB)

References

  1. Using @Path in RESTEasy
  2. JAX-RS @Path

10 comments on “JAX-RS @Path URI matching example

  1. Hello,
    How can I implement the following:
    /users/{userId}
    /users?username={username}

    I already did an implementation on this but when I call /users/{userId} the second method gets called first.
    Thank you in advance.

    Reply
  2. Hi in your second example you have given /users/mkyong, it works fine, What if I want it work directly on /mkyong . I am trying to achive this http://www.abc.com/mkyong. Can you please help me with this.

    Reply
  3. Hi,
    I’m new to JAX-RS. In my application when the URI is not correct, it throws 404 exception back. I need to handle this exception when container cant match any resource(method) to handle the request. Is it possible? If yes, how can we do it? Thanks.

    Reply
    1. Hey mate,
      There’s an example shown here which doesnt have a method annotated with @Post, which acts as a default method entry level. Just make sure, you use Response.status(404)….

      Reply
  4. Hi there! I just wanted to ask if you ever have
    any issues with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no backup.
    Do you have any solutions to stop hackers?

    Reply
  5. URI Pattern : ?/users/username/aaa? , failed, don?t match ?[a-zA-Z][a-zA-Z_0-9]?, first character need ?[a-zA-Z]?, second character need ?[a-zA-Z_0-9]?.

    —-
    i think this is wrong, since “aaa” matches the pattern [a-zA-Z_0-9].

    the request to “/users/username/aaa” should return true

    Reply
    1. [a-zA-Z][a-zA-Z_0-9] there are 2character, if you use “/users/username/aa” it will work, if you want to use “/users/username/aaa” you have to change like this

      @Path(“/username/{username : [a-zA-Z][a-zA-Z_0-9]+}”)

      Reply
      1. you wont need initial offset [a-zA-Z] also for /aaa if you have given [a-zA-Z_0-9]+

        Reply
    2. Yes Brother Above regular expression matches aaa input
      may be content writer mistake it should [a-zA-Z][A-Z_0-9] if second character support Capital Letters and Numbers
      if only numbers [a-zA-Z][0-9]

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *