JAX-RS允许使用正则表达式匹配PathParams,如下所示:
@GET
@Path("/users/{username : [a-zA-Z][a-zA-Z_0-9]}")
public Response getUserByUserName(@PathParam("username") String username) {
...
}
但它是否允许以某种方式匹配QueryParams?
答案 0 :(得分:2)
不允许为QueryParams指定正则表达式。解决方法:只需定义您自己的Java数据类型,该数据类型将用作此查询参数的表示形式,如果您必须处理String
s
String
答案 1 :(得分:1)
不幸的是,没有这种保护QueryParams的方法。 您可以做的是在服务器端类上使用辅助方法来检查它们的值。
@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
}