JAX-RS: Multiple paths

后端 未结 3 1461
耶瑟儿~
耶瑟儿~ 2020-12-05 01:43

Is it possible to do something like that?

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

public class xxx
{
  @GET
  @Path(value = \"path1\")
  public Res         


        
3条回答
  •  Happy的楠姐
    2020-12-05 02:40

    Some extra details about Path annotation...

    As a previous responses state, regular expressions to be used with in the annotated path declaration mapping:

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

    You can declare multiple paths, but there is also a path hierarchy that was not immediately obvious to me whereby the class annotated path prefixes the following method path annotations. One might write the following class for a concise multiple path option which could be useful for resource versioning perhaps.

    @Path("/{a:v1|v2}")
    @Produces("text/*")
    public class BlahResource {
    
        @GET
        @Path("/blah")
        public Response m1() {
            return Response.ok("blah").build();
        }
    }
    

    Please note the fact that the class "BlahResource" has been declared with the path "/v1" or "/v2" making the resource accessible as:

    $ curl localhost:8080/v1/blah
    blah
    

    and also

    $ curl localhost:8080/v2/blah
    blah
    

提交回复
热议问题