如何在Java Jersey REST服务中强制使用queryparams?

时间:2012-12-06 07:58:11

标签: jersey

我有一个REST API,可以接受3个查询参数。在没有任何一个查询参数的情况下调用查询时,API将执行并返回结果。我们如何强制使用queryparams?如何添加验证以检查是否存在所有参数?另外,请告诉我最好的方法。

2 个答案:

答案 0 :(得分:0)

在一个非常简单的层面上,您可以注入HttpServletRequest并自行检查:

@GET
public Response example(@Context HttpServletRequest request, 
                        @QueryParam("name") String name) {
  if (null == request.getParameter("name")) {
    ResponseBuilder builder = Response.status(404);
    return builder.build();
  }
  // Do something with name
}

或者您可以使用AOP实现更精细的功能。 Here's一篇关于更多选项的博文。

答案 1 :(得分:0)

jersey并未提供开箱即用的强制参数检查功能。但是你可以做一些像实现自己的注释来实现它。 以下是注释代码:

@Target(value = ElementType.METHOD)  
 @Retention(value = RetentionPolicy.RUNTIME)  
 public @interface Required {  
    String[] value();  
 }  

您还需要一个过滤器,下面是代码:

 public class RequiredParamResourceFilterFactory implements ResourceFilterFactory {  
   @Context  
   private transient HttpServletRequest servletRequest;  
   private class RequiredParamFilter implements ResourceFilter, ContainerRequestFilter {  
     private final String[] requiredParams;  
     protected List<String> parametersValueMissing;  
     private RequiredParamFilter(String[] requiredParams) {  
       this.requiredParams = requiredParams;  
     }  
     @Override  
     public ContainerRequest filter(ContainerRequest containerRequest) {  
       boolean missingMandatoryParameter = false;  
       List<String> missingParameters = new ArrayList<String>();  
       List<String> requiredParametersValueMissing = new ArrayList<String>();  
       List<String> URLParameters = getURLParameters(containerRequest.getQueryParameters());  
       List<String> methodRequiredParameters = Arrays.asList(requiredParams);  
       if (methodRequiredParameters != null) {  
         for (String methodRequiredParam : methodRequiredParameters) {  
           if (URLParameters == null) {  
             missingMandatoryParameter = true; //we will check this flag before returning result set to caller  
             missingParameters.add(methodRequiredParam);  
           } else if (!URLParameters.contains(methodRequiredParam)) {  
             missingMandatoryParameter = true; //we will check this flag before returning result set to caller  
             missingParameters.add(methodRequiredParam);  
             //Add to required parameters value missing List, only if the parameter is mandatory and value is not provided  
             // in the URL  
           } else if (parametersValueMissing.contains(methodRequiredParam)) {  
             requiredParametersValueMissing.add(methodRequiredParam);  
           }  
         }  
         if (missingMandatoryParameter && requiredParametersValueMissing.size() > 0) {  
           throw new YourCustomException("Missing Parameters = " + StringHelper.ArrayToString(missingParameters) +  
               "\nParameter value missing for " + StringHelper.ArrayToString(requiredParametersValueMissing));  
         } else if (missingMandatoryParameter) {  
           throw new YourCustomException("Missing Parameters = " + StringHelper.ArrayToString(missingParameters), MisbarErrorCode.VALIDATION_WRONG_INPUT_ERROR, "Customers");  
         } else if (requiredParametersValueMissing != null &&  
             requiredParametersValueMissing.size() > 0) {  
           throw new YourCustomException("Parameter value missing for " + StringHelper.ArrayToString(requiredParametersValueMissing));  
         }  
       }  
       return containerRequest;  
     }  
     @Override  
     public ContainerRequestFilter getRequestFilter() {  
       return this;  
     }  
     @Override  
     public ContainerResponseFilter getResponseFilter() {  
       return null;  
     }  
     /**  
      * To fetch the parameters sent to webservice call, these will be used to find if required parameter  
      * are present or not  
      *  
      * @param queryParams the queryparams sent  
      * @return all the parameters sent in URL  
      */  
     private List<String> getURLParameters(MultivaluedMap<String,String> queryParams) {  
       parametersValueMissing = new ArrayList<String>();  
       List<String> arr = new ArrayList<String>();  
       for(String key:queryParams.keySet())  
       {  
         arr.add(key);  
         if(queryParams.get(key)==null)  
           parametersValueMissing.add(key);  
       }  
       if(!arr.isEmpty())  
         return arr;  
       return null;  
     }  
   }  
   @Override  
   public List<ResourceFilter> create(AbstractMethod am) {  
     Required required = am.getAnnotation(Required.class);  
     if(required!=null)  
     {  
       return Collections.<ResourceFilter>singletonList(new RequiredParamFilter(required.value()));  
     }  
     return null;  
   }  
 }  

下面的示例显示了如何使用此注释,因此在下面的webservice中; file_id和count是必需参数:

@GET  
   @Produces(MediaType.APPLICATION_JSON+";charset=utf-8")  
   @Cacheable(isCacheable = true)  
   @Path("posts/clusters")  
   @Required({"file_id","count"})  
   @Timed  
   public Response getClusters(  
       @QueryParam("file_id") Integer fileId,  
       @QueryParam("count") Integer count,  
       @DefaultValue("-1")@QueryParam("start_time") Long startTime){  
 ;  
 }  

如果webservice调用中未提供必需参数,则会收到如下错误,提及缺少的参数名称:

{
message: "Missing Parameters = file_id, count",
errorCode: "600"
}

希望这能解决你的问题。