JavaEE Web JAX-RS:如何匹配查询参数
JavaEE Web JAX-RS: how to match Query params
JAX-RS 允许像这样使用正则表达式匹配 PathParams:
@GET
@Path("/users/{username : [a-zA-Z][a-zA-Z_0-9]}")
public Response getUserByUserName(@PathParam("username") String username) {
...
}
但是它是否也允许以某种方式匹配 QueryParams?
您不能为 QueryParams 指定正则表达式。作为一种解决方法:只需定义您自己的 Java 数据类型,该数据类型将用作此查询参数的表示,然后将其转换为 String
如果您必须处理 String
s
不幸的是,没有这种保护 QueryParams 的方法。
你可以做的是在服务器端使用辅助方法 class 来检查它们的值。
@GET
@Path("/myPath")
public Response yourMethod(@QueryParam("code") long code, @QueryParam("description") String desc){
if(isParametersValid(code,desc)){
//proceed with your logic
}else{
return Response.status(Response.Status.FORBIDDEN).entity("The passed parameters are not acceptable").build();
}
}
private boolean isParametersValid(long code, String desc){
//check
}
JAX-RS 允许像这样使用正则表达式匹配 PathParams:
@GET
@Path("/users/{username : [a-zA-Z][a-zA-Z_0-9]}")
public Response getUserByUserName(@PathParam("username") String username) {
...
}
但是它是否也允许以某种方式匹配 QueryParams?
您不能为 QueryParams 指定正则表达式。作为一种解决方法:只需定义您自己的 Java 数据类型,该数据类型将用作此查询参数的表示,然后将其转换为 String
如果您必须处理 String
s
不幸的是,没有这种保护 QueryParams 的方法。 你可以做的是在服务器端使用辅助方法 class 来检查它们的值。
@GET
@Path("/myPath")
public Response yourMethod(@QueryParam("code") long code, @QueryParam("description") String desc){
if(isParametersValid(code,desc)){
//proceed with your logic
}else{
return Response.status(Response.Status.FORBIDDEN).entity("The passed parameters are not acceptable").build();
}
}
private boolean isParametersValid(long code, String desc){
//check
}