Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC-如何检查未传递任何意外的查询字符串参数?_Java_Spring_Spring Mvc_Validation - Fatal编程技术网

Java SpringMVC-如何检查未传递任何意外的查询字符串参数?

Java SpringMVC-如何检查未传递任何意外的查询字符串参数?,java,spring,spring-mvc,validation,Java,Spring,Spring Mvc,Validation,我有一个用SpringMVC编写的web服务。它可以由第三方开发人员使用。 我们的方法有很多可选的参数(在查询字符串中传递) 我希望确保所有查询字符串参数拼写正确,并且没有拼写错误。 有没有简单的方法?方法签名示例: @RequestMapping(value = {"/filter"}, method = RequestMethod.GET) @ResponseBody public List<MetricType> getMetricTypes(

我有一个用SpringMVC编写的web服务。它可以由第三方开发人员使用。 我们的方法有很多可选的参数(在查询字符串中传递)

我希望确保所有查询字符串参数拼写正确,并且没有拼写错误。 有没有简单的方法?方法签名示例:

 @RequestMapping(value = {"/filter"}, method = RequestMethod.GET)
    @ResponseBody
    public List<MetricType> getMetricTypes(    
            @RequestParam(value = "subject", required = false) Long subjectId,
            @RequestParam(value = "area", required = false) Long areaId,
            @RequestParam(value = "onlyImmediateChildren", required = false) Boolean onlyImmediateChildren,   
            @RequestParam(value = "componentGroup", required = false) Long componentGroupId    
            ) throws Exception
    {
        //Some code
    }
@RequestMapping(值={“/filter”},方法=RequestMethod.GET)
@应答器
公共列表getMetricTypes(
@RequestParam(value=“subject”,required=false)长主语,
@RequestParam(value=“area”,required=false)长区域ID,
@RequestParam(value=“onlyImmediateChildren”,required=false)布尔onlyImmediateChildren,
@RequestParam(value=“componentGroup”,required=false)长componentGroupId
)抛出异常
{
//一些代码
}
如果有人用“onlyImmediateChildren=true”参数(输入错误)而不是“onlyImmediateChildren=true”调用此方法,Spring MVC将忽略输入错误的参数,并假定“onlyImmediateChildren”为空。开发人员将得到稍微不正确的结果列表,并且不会注意到错误。这些问题可能很普遍,很难诊断。我想检查查询字符串中是否有键入的参数,以防止出现此类问题

更新

可以从查询字符串中提取实际参数列表。然后可以将其与允许的参数列表进行比较。如果我硬编码允许的参数列表,它将复制方法签名。我想知道从方法签名(例如通过@RequestParam注释)中提取允许的参数列表是否容易

非常感谢

Maxim

您可以使用请求的方法获取所有提交参数的
映射,并根据所有允许参数的列表验证键。只需将
请求
对象添加到方法签名中,即可获得该对象,例如:

public List<MetricType> getMetricTypes(   
    HttpServletRequest request,
    @RequestParam(value = "subject", required = false) Long subjectId,
    ...
) throws Exception {
公共列表getMetricTypes(
HttpServletRequest请求,
@RequestParam(value=“subject”,required=false)长主语,
...
)抛出异常{
您可以使用请求的方法获取所有提交参数的
映射
,并根据所有允许参数的列表验证密钥。您应该能够通过简单地将
请求
对象添加到方法签名中来获取该对象,例如:

public List<MetricType> getMetricTypes(   
    HttpServletRequest request,
    @RequestParam(value = "subject", required = false) Long subjectId,
    ...
) throws Exception {
公共列表getMetricTypes(
HttpServletRequest请求,
@RequestParam(value=“subject”,required=false)长主语,
...
)抛出异常{

您可以实现自己的。在预处理方法中,您可以获得所有用@RequestParameter注释的HandlerMethod参数。这些都是请求中允许的参数。

您可以实现自己的参数。在预处理方法中,您可以获得所有用@RequestParameter注释的HandlerMethod参数。这些参数都将是allo请求中的wed参数。

Spring将通过类型的参数注入url字符串中的所有查询参数

控制器方法中的
@RequestParam Map
,如果存在

@RequestMapping(value = "", method = RequestMethod.GET, produces = {"application/json"})
 public HttpEntity<PagedResources<WebProductResource>> findAll(@RequestParam Map<String, String> allRequestParams){
...
@RequestMapping(value=”“,method=RequestMethod.GET,products={“application/json”})
公共HttpEntity findAll(@RequestParam-Map-allRequestParams){
...
}


然后,您可以自己验证映射的键。要获得一般性的“enterprisey”方法,请参见我的回答:

Spring将通过type参数注入url字符串中的所有查询参数

控制器方法中的
@RequestParam Map
,如果存在

@RequestMapping(value = "", method = RequestMethod.GET, produces = {"application/json"})
 public HttpEntity<PagedResources<WebProductResource>> findAll(@RequestParam Map<String, String> allRequestParams){
...
@RequestMapping(value=”“,method=RequestMethod.GET,products={“application/json”})
公共HttpEntity findAll(@RequestParam-Map-allRequestParams){
...
}


然后,您可以自己验证映射的键。有关一般性的“enterprisey”方法,请参见我的回答:

这是我的
HandlerInterceptor
实现,它只接受由参数注释明确定义的参数:

import javax.servlet.http.HttpServletRequest
导入javax.servlet.http.HttpServletResponse
导入org.springframework.http.HttpStatus
导入org.springframework.stereotype.Component
导入org.springframework.web.bind.annotation.RequestParam
导入org.springframework.web.method.HandlerMethod
导入org.springframework.web.servlet.HandlerInterceptor
/**
*拦截器,确保只发送预期的[RequestParam]。
*/
@组成部分
类UnexpectedParameterHandler:HandlerInterceptor{
重写fun预处理(请求:HttpServletRequest,响应:HttpServletResponse,处理程序:Any):布尔值{
if(handler是handler方法){
val queryParams=request.parameterNames.toList()
val expectedParams=handler.methodParameters
.map{methodParameter->
val requestParamName=methodParameter.getParameterAnnotation(RequestParam::class.java)?.name
val parameterName=methodParameter.parameter.name
requestParamName?:参数名称
}
val unknownParameters=queryParams.减号(expectedParams)
if(unknownParameters.isNotEmpty()){
response.writer.write(“意外参数$unknownParameters”)
response.status=HttpStatus.BAD_REQUEST.value()
返回错误
}
}
返回super.preHandle(请求、响应、处理程序)
}
}

以下是我对
HandlerInterceptor
的实现,它只接受由参数注释明确定义的参数:

import javax.servlet.http.HttpServletRequest
导入javax.servlet.http.Htt