Jakarta ee 在JAX-RS中通过GET参数映射不同的路由

Jakarta ee 在JAX-RS中通过GET参数映射不同的路由,jakarta-ee,jax-rs,Jakarta Ee,Jax Rs,在Spring MVC中,可以为相同的基本URI映射不同的路由(用@*Mapping系列注释的方法),但使用不同的GET参数,例如: @GET @Path("/test") public Response getResponse( @QueryParam("param1") int param1, @RestController @请求映射(路径=“/test”) 公共类测试控制器{ @GetMapping(params=“myParam=1”) 公共无

在Spring MVC中,可以为相同的基本URI映射不同的路由(用
@*Mapping
系列注释的方法),但使用不同的GET参数,例如:

    @GET
    @Path("/test")
    public Response getResponse(
        @QueryParam("param1") int param1,
@RestController
@请求映射(路径=“/test”)
公共类测试控制器{
@GetMapping(params=“myParam=1”)
公共无效路径1(){
//当GET param“myParam”的值为1时调用
}
@GetMapping(params=“myParam=2”)
公共无效路径2(){
//当GET param“myParam”的值为2时调用
}
//很好!
}
我试图用JAX-RS实现相同的路由,但找不到内置的方法

@Path(“/test”)
公共类测试控制器{
@得到
公共无效路径1(){
//当GET param“myParam”的值为1时应调用
}
@得到
公共无效路径2(){
//当GET param“myParam”的值为2时应调用
}
//丢失的那块是什么?
}
您使用QueryParam:

@QueryParam("param1") int param1
例如:

    @GET
    @Path("/test")
    public Response getResponse(
        @QueryParam("param1") int param1,
您还可以在
@Path


@Path(“/users/{id:[0-9]*}”)

JAX-RS没有您要查找的映射类型-没有基于查询参数的匹配方法。通常,模式是基于路径的。因此,在您的示例中,JAX-RS的想法更像:

@Path("/test")
public class TestController {

    @GET
    @Path("/myParam/1")
    public void path1() {
        // will be called when the url ends in /test/myParam/1
    }

    @GET
    @Path("/myParam/2")
    public void path2() {
        // will be called when the url ends in /test/myParam/2
    }
}
然而,说到这里并扩展@ACV的答案,您还可以做如下操作:

@Path("/test")
public class TestController {

    @GET
    public Response routeRequest(@QueryParam("myParam") int myParam) {
        if( myParam == 1 )
            path1();
        else if( myParam == 2 )
            path2();
       // handle bad myParam

      return Response.status(Response.Status.OK).build();
    }

    private void path1() {
        // will be called when GET query param "myParam" present with value 1
    }

    private void path2() {
        // will be called when GET query param "myParam" present with value 2
    }
}
或者是一个基于路径的示例,非常类似于上述内容:

@Path("/test")
public class TestController {

    @GET
    @Path("/myParam/{id}")
    public Response routeRequest(@PathParam("id") int myParam) {
        if( myParam == 1 )
            path1();
        else if( myParam == 2 )
            path2();
       // handle bad myParam

      return Response.status(Response.Status.OK).build();
    }

    private void path1() {
        // will be called when GET path param "myParam" present with value 1
    }

    private void path2() {
        // will be called when GET path param "myParam" present with value 2
    }
}

谢谢你的回答
@QueryParam
确实会检索该值,但这不会自动影响路由。请检查我的答案,您可以使用regex筛选路径@kagmole谢谢您的回答。你刚刚证实了我的恐惧。最后,我用固定值而不是
{id}
完成了基于路径的示例。