繁体   English   中英

Spring MVC - 如何获取@RequestMethod方法参数列表(可能是@RequestParam属性)

[英]Spring MVC - How to get list of @RequestMethod method parameters (possibly by @RequestParam attribute)

我有一个Spring MVC控制器,使用以下方法:

    @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
    }

是否有可能以编程方式获取当前方法的参数列表(例如通过@RequestParam注释)? 解决方案应该没有调试符号。 如果需要,可以对方法名称进行硬编码。

我试图检查查询字符串是否包含无效参数(例如因为拼写错误)。 我将比较请求中的查询参数名称和方法签名中的查询参数名称(如果可能)。

更新

解决方案也适用于可选(required = false)参数。

非常感谢

格言

你可以使用HttpServletRequest request

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  ,
HttpServletRequest request  
            ) throws Exception

然后获取所有请求参数名称

request.getParameterNames()

并使用循环来完成你的工作

for ( String parameterName:requestParameterNames){

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM