QueryParams上的JAVA EE Rest条件GET方法
我希望在我的Rest资源类上有两个GET方法。 如果查询参数的值为1,第二个值为2,则会做出反应 让我们说:QueryParams上的JAVA EE Rest条件GET方法,java,rest,get,query-parameters,Java,Rest,Get,Query Parameters,我希望在我的Rest资源类上有两个GET方法。 如果查询参数的值为1,第二个值为2,则会做出反应 让我们说: @Path("/myApi") public class MyApiService { @GET @Produces(MediaType.APPLICATION_JSON) public Response methodOne(...) { ... return ...; } @GET @P
@Path("/myApi")
public class MyApiService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response methodOne(...) {
...
return ...;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response methodTwo(...) {
...
return ...;
}
如何实现查询参数的条件路由
我希望methodOne在QueryParam为?type=1时作出反应,Method2在QueryParam为?type=2时作出反应。基于QueryParam选择servlet处理程序不是一个好方法,默认情况下,没有库给您这样做的机会。 我想到的最接近的是PathParam,它类似于Path\api\{param1}\{param2},但它不是您想要的 为了达到你想要的只是 如果在queryparam选择范围之外不需要这些方法,请将它们注销为servlet处理程序(可选) 定义一个将根据查询参数进行选择的新参数 @得到 @ProducesMediaType.APPLICATION_JSON
public Response methodThree(QueryParam('type') String type) {
return type.equals("type1") ? this.methodOne() : this.methodTwo();
}
对于同一路径,不能有两个参数相同的方法。 这不漂亮,但会有用的
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod(@QueryParam("type") String type){
if(type.equals("one"))
return methodOne();
else
return methodTwo();
}
这是我认为是的选项,我认为这是唯一的选项:你能告诉我们为什么你认为这是必要的吗?一个选项是使用@Tichodroma,我想分离方法,所以在例如swagger中,你可以看到更多的方法。我认为每个不同查询参数的方法并不像看起来那么愚蠢。。。