Spring 4.1.7 验证请求体 [英] Spring 4.1.7 validate request body

查看:39
本文介绍了Spring 4.1.7 验证请求体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题在其他帖子中已经存在,但是在应用了建议的修复后不起作用.

I know this issue has been around there in other post, but after applying the fiux suggested was not working.

我使用的是 spring 4.1.7 版本,我想从 post rest 调用中验证 RequestBody.为此,我尝试遵循一组代码,但没有按我预期的那样工作.

I am using spring 4.1.7 version, i want to validate the RequestBody from post rest call. For doing this i tried following set of codes, but it was not working as i expected.

我的请求正文 pojo 类.

My Request body pojo classes.

ParentPojo.class

ParentPojo.class

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@Validated
public class ParentPojo<T> implements Serializable{
    @NotNull
    private String  cNumber;
    @NotNull
     private String  statusCode;
     @NotNull T child;
}

ChildPojo.class

ChildPojo.class

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@Validated
public class ChildPojo{

    @NotNull
    private String name;

    @NotNull
    private String address;

    @NotNull
    private String pin;

}

控制器:

只添加方法

@Autowired
    @Qualifier("validator")
    private Validator validator;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        binder.setValidator(validator);
    }

 @RequestMapping(produces = { "application/json", "application/xml" }, consumes ={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = { RequestMethod.POST })
    @ResponseStatus(HttpStatus.CREATED)
    public Messageresponse<ChildPojo> create(@NotNull(groups = {ParentPojo.class, ChildPojo.class}) 
    @Valid @Validated({ParentPojo.class, ChildPojo.class}) @RequestBody final ParentPojo<ChildPojo> ParentPojo, BindingResult bindingResult) {
       System.out.println("new version 8="+bindingResult.hasErrors());
       validator.validate(ParentPojo, bindingResult);
       if(bindingResult.hasErrors()) {
           System.out.println("Non formated form stuff.");
       }

        return service.create(ParentPojo);
    }

 @RequestMapping(value = "/{create}", produces = { "application/json", "application/xml" }, consumes ={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = { RequestMethod.POST })
    @ResponseStatus(HttpStatus.CREATED)
    public Messageresponse<ChildPojo> create1(@NotNull @Valid @RequestBody final ParentPojo<ChildPojo> ParentPojo, BindingResult bindingResult) {
       System.out.println("new version 8="+bindingResult.hasErrors());
       validator.validate(ParentPojo, bindingResult);
       if(bindingResult.hasErrors()) {
           System.out.println("Non formated form stuff.");
       }

        return service.create(ParentPojo);
    }

应用上下文xml:

<mvc:annotation-driven validator="validator">
    </mvc:annotation-driven>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

jar 文件已尝试:

hibernate-validator-4.3.0.Final
hibernate-validator-4.1.0.Final
hibernate-validator-4.0.2.GA
validation-api-1.1.0.Final
spring-context-4.1.7.RELEASE

但是对于下面的请求,上述所有组合都不起作用:

But nothing was working with all the above combination for the request below:

在下面的请求pin"中丢失,控制器方法@Valid @RequestBody 期望将此请求作为错误请求处理.相反,它正在接受请求正文并进一步处理.

in below request "pin" is missed and the controller method @Valid @RequestBody expected to handle this request as Bad Request. instead it is accepting the request body and processing further.

{
"cNumber" : "ff",
"statusCode" : "ddd",
"child" : {
"name" : "ll",
"address" : "ll"
}

推荐答案

看这个问题 这里

您需要将子 pojo 装饰为 @Valid

You need to decorate the child pojo as @Valid

public class ParentPojo<T> implements Serializable{
   @NotNull
   private String  cNumber;
   @NotNull
   private String  statusCode;
   @Valid 
   @NotNull 
   T child;
}

这篇关于Spring 4.1.7 验证请求体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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