简体   繁体   English

Java Spring - 如何处理缺少的必需请求参数

[英]Java Spring - how to handle missing required request parameters

Consider the following mapping: 考虑以下映射:

@RequestMapping(value = "/superDuperPage", method = RequestMethod.GET)
public String superDuperPage(@RequestParam(value = "someParameter", required = true) String parameter)
{
    return "somePage";
}

I want to handle the missing parameter case by not adding in required = false . 我想通过添加required = false来处理缺少的参数大小写。 By default, 400 error is returned, but I want to return, let's say, a different page. 默认情况下,返回400错误,但我想返回一个不同的页面。 How can I achieve this? 我怎样才能做到这一点?

If a required @RequestParam is not present in the request, Spring will throw a MissingServletRequestParameterException exception. 如果需要@RequestParam中不存在请求,Spring会抛出一个MissingServletRequestParameterException例外。 You can define an @ExceptionHandler in the same controller or in a @ControllerAdvice to handle that exception: 您可以在同一个控制器或@ControllerAdvice定义@ExceptionHandler来处理该异常:

@ExceptionHandler(MissingServletRequestParameterException.class)
public void handleMissingParams(MissingServletRequestParameterException ex) {
    String name = ex.getParameterName();
    System.out.println(name + " parameter is missing");
    // Actual exception handling
}

I want to return let's say a different page. 我想回来让我们说一个不同的页面。 How to I achieve this? 我怎么做到这一点?

As the Spring documentation states: 正如Spring文档所述:

Much like standard controller methods annotated with a @RequestMapping annotation, the method arguments and return values of @ExceptionHandler methods can be flexible . 与使用@RequestMapping注释注释的标准控制器方法非常相似@ExceptionHandler方法的方法参数和返回值可以是灵活的 For example, the HttpServletRequest can be accessed in Servlet environments and the PortletRequest in Portlet environments. 例如,可以在Servlet环境中访问HttpServletRequest ,在Portlet环境中访问PortletRequest The return type can be a String , which is interpreted as a view name , a ModelAndView object, a ResponseEntity , or you can also add the @ResponseBody to have the method return value converted with message converters and written to the response stream. 返回类型可以是String ,它被解释为视图名称ModelAndView对象, ResponseEntity ,或者您还可以添加@ResponseBody以使用消息转换器转换方法返回值并将其写入响应流。

An alternative 替代

If you use the @ControllerAdvice on your class and if it extends the Spring base class ResponseEntityExceptionHandler . 如果在类上使用@ControllerAdvice ,并且它扩展了Spring基类ResponseEntityExceptionHandler A pre-defined function has been created on the base class for this purpose. 为此,已在基类上创建了预定义函数。 You have to override it in your handler. 你必须在你的处理程序中覆盖它。

    @Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    String name = ex.getParameterName();
    logger.error(name + " parameter is missing");

    return super.handleMissingServletRequestParameter(ex, headers, status, request);
}

This base class is very useful, especially if you want to process the validation errors that the framework creates. 此基类非常有用,尤其是当您要处理框架创建的验证错误时。

You can do this with Spring 4.1 onwards and Java 8 by leveraging the Optional type. 您可以使用Spring 4.1以及Java 8通过利用Optional类型来完成此操作。 In your example that would mean your @RequestParam String will have now type of Optional<String> . 在您的示例中,这意味着您的@RequestParam String现在将具有Optional<String>类型。

Take a look at this article for an example showcasing this feature. 请查看本文 ,以获取展示此功能的示例。

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

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