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

查看:172
本文介绍了空请求正文未被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。当我访问传递给save的项时,我得到 NullPointerException 因为item为null。我已经尝试使用 @RequestBody 的required参数,但只检查是否有POST的内容。如上所述,只有当用户在没有引号作为请求主体的情况下发布null时,才会传递空项。是否有自动的Spring注释或配置来阻止此null被传递,或者我应该只是将 if(item == null)抛出新的Exception(); in all我的控制器会出现什么情况?

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天全站免登陆