Spring 4.1.7验证请求主体

用户2000189

我知道这个问题已经在其他帖子中出现了,但是在应用了flux之后,建议无法正常工作。

我正在使用Spring 4.1.7版本,我想从休息后调用验证RequestBody。为此,我尝试遵循以下代码集,但未按预期工作。

我的请求正文pojo类。

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

@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

但是,对于以下请求,上述所有组合均无法正常工作:

在下面的请求中,缺少“ pin”,并且控制器方法@Valid @RequestBody应该将此请求作为错误请求进行处理。相反,它正在接受请求正文并进一步处理。

{
"cNumber" : "ff",
"statusCode" : "ddd",
"child" : {
"name" : "ll",
"address" : "ll"
}
阿曼纽尔·内加(Amanuel Nega)

这里看这个问题

您需要将儿童pojo装饰为 @Valid

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

验证Spring Boot控制器中HTTP POST请求中的请求主体是否为null

Spring 4验证器NoClassDefFoundError

Spring 4中的@PathVariable验证

Spring Integration 4异步请求/响应

如何向Spring MockMVC添加请求主体

学习 Spring 4 和 JEE 7?

如何在Spring Controller中请求请求的来源和主体

Spring Data Neo4j 4 Repository方法返回1个元素

Symfony 4扩展了验证请求

Spring 4 Ajax登录重定向到请求的URL

Spring 4 RestController 未正确映射 xml 请求

Spring Controller:对象='invoiceData'的验证失败。错误计数:4

如何在Spring 4中打开注释驱动的验证?

使用neo4j进行Spring身份验证

Spring Boot在错误响应实体中添加请求主体

如何在Spring MVC中解码Gzip压缩的请求主体

Spring Rest请求中的单个字段主体

Spring MVC:不反序列化JSON请求主体

Spring Boot RestController-PostMapping-将请求主体作为InputStream

Spring无法反序列化请求主体

jsf 2.2 +使用Spring验证器和JSF语言环境的Spring 4 JSR-349

Spring RestTemplate:如何到达检查#is4xxClientError的状态,而不是RestClientException 1st?

为什么通过Spring加载log4j2.xml会为log4j1类抛出NoClassDefFoundError?

在最新的Spring v4中将@RequestBody与可选主体一起使用

如何从Spring 4 stomp Websocket方法获取/设置主体和会话属性

XSD验证1到4个特定字符的列表

自定义验证Spring请求参数

Spring 4,JBoss 7,@Configuration Bean等效的XML-Spring Transactions

Spring安全性遇到“为此主体超过最大会话数为1”的问题。