空请求正文没有被 Spring @RequestBody @Valid 注释捕获 [英] Null request body not getting caught by Spring @RequestBody @Valid annotations

查看:39
本文介绍了空请求正文没有被 Spring @RequestBody @Valid 注释捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到了 Spring 中 @RequestBody 注释的问题.我目前在我的模型上正确设置了所有验证注释,并且在发布对象时它们工作得很好.即使发布的请求正文完全为空或空对象{}",一切都按预期工作.当有人尝试发布null"的请求正文时,就会出现问题.这以某种方式通过了 @Valid 注释并且没有被捕获,当我尝试访问该对象时导致 NullPointerException .我在下面粘贴了我的控制器的片段.

I am currently running into an issue with the @RequestBody annotation in Spring. I currently have all my validation annotations set up on my models properly, and they work great when an object is POSTed. Everything works as expected even when the request body posted is completely empty or an empty object "{}". The problem arises when someone tries to post a request body of "null". This somehow gets through the @Valid annotation and is not caught, causing a NullPointerException when I try to access the object. I have pasted a snippet of my controller below.

@Secured({ ROLE_ADMIN })
@RequestMapping(method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE } )
public HttpEntity<Resource<Item>> addItem(@RequestBody @Valid Item item) throws Exception {
    Resource<Item> itemResource = this.itemResourceAssembler.toResource(this.itemService.save(item));
    return new ResponseEntity<Resource<Item>>(itemResource, HttpStatus.CREATED);
}

我在 itemService.save() 中做了一些检查,看看数据库中是否已经存在一个项目,因为它被用作 upsert.当我访问传递给保存的项目时,我得到 NullPointerException 因为项目为空.我已经尝试使用 @RequestBody 的必需"参数,但这只检查是否有发布的东西.如上所述,只有当用户发布不带引号的null"作为请求正文时,才会传递空项目.是否有一个自动化的 Spring 注释或配置来阻止传递这个 null,或者我应该把 if(item == null) throw new Exception(); 放在我所有的控制器中,这可以突然出现?

I do some checks in itemService.save() to see if an item exists in the database already, since this is being used as an upsert. When I access the item passed to save I get the NullPointerException because item is null. I've tried using the "required" parameter of @RequestBody but that only checks to see if there was something POSTed. As stated above, the null item is only passed through when a user posts "null" without the quotes as the request body. Is there a automated Spring annotation or configuration to stop this null from being passed through, or should I just put if(item == null) throw new Exception(); in all of my controllers where this could crop up?

推荐答案

从您发布的代码片段的外观来看,您有一个明确定义的服务层.我建议在您的服务中使用方法级别验证,如下所示

By the looks of the code snippet you posted, you have a clearly defined service layer. I would suggest using method level validation in you service as follows

@Validated
public interface ItemService {

public Item save(@NotNull Item item);

如果您使用 spring 的 XML 配置,请将其添加到您的应用程序的上下文 xml.

If your using spring's XML config add this to your app's context xml.

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>

将抛出 MethodConstraintViolationException,如果您定义了该异常,则该异常可能会在 @ControllerAdvice 中捕获.

a MethodConstraintViolationException will be thrown that could be caught in a @ControllerAdvice if you have that defined.

@ExceptionHandler(MethodConstraintViolationException.class)

快乐编码!

这篇关于空请求正文没有被 Spring @RequestBody @Valid 注释捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆