Spring 3 - 如何在传递其他请求参数时抛出异常

时间:2015-09-11 08:52:51

标签: java spring spring-mvc

我有一个控制器映射,我在其中传递2个请求参数而不是1.但是当完成时,Spring没有抛出任何异常,而是忽略了额外的请求参数。

例如:

@RequestMapping(value="/test", method = RequestMethod.GET)
public ModelAndView eGiftActivation(@RequestParam("value") String value)

当我使用/test.do?value=abcd点击我的应用时,它运行正常。但是当我传递像/test.do?value=abcd&extra=unwanted这样的其他参数时,它也能正常工作。

在这种情况下,我希望Spring限制传递其他参数的第二个URL。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

我更喜欢在过滤器内部处理这些验证(如果需要,可能是什么原因),以便请求甚至不会达到Controller方法。

请在下面找到处理Filter内部所需的代码(逻辑与Slava几乎相似)。

 @Component
 public class InvalidParamsRequestFilter extends OncePerRequestFilter {

 @Override
 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        Map<String, String[]> params = request.getParameterMap();

        if (request.getRequestURI().contains("/test") && (params.size() != 1    || !params.containsKey("value"))) {

            //Here, Send back the Error Response OR Redirect to Error Page

        } else {        
            filterChain.doFilter(request, response);
        }
     }
 }
相关问题