[英]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, theHttpServletRequest
can be accessed in Servlet environments and thePortletRequest
in Portlet environments. 例如,可以在Servlet环境中访问HttpServletRequest
,在Portlet环境中访问PortletRequest
。 The return type can be aString
, which is interpreted as a view name , aModelAndView
object, aResponseEntity
, 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.