【问题标题】:@NotNull validation on collection with Jackson@NotNull 验证与 Jackson 的集合
【发布时间】:2017-09-27 15:51:10
【问题描述】:

当我发布到我的控制器时,我遇到了可清除异常的问题 - 带有 null 的 json。

Ive set all fields as @NotNull. And its 工作正常(哪些 null 值我看到了很好的异常消息:字段 '' 上的对象 '' 中的字段错误:拒绝值 [null])。

很遗憾,使用 @NotNull 收集字段会返回 JsonMappingException。

例如:

  @Value
public final class User {

    @NotNull
    private final Integer age; //work fine


    @NotNull
    private final List<Books> books; //doesn`t work

ve also tried with @Valid and @NotEmpty, but it didnt 帮助。

我的控制器方法:

@Timed
    @ExceptionMetered
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @PreAuthorize("@auth.hasAccess(...)")
    void registerUser(@RequestBody @Valid User user) {
        userService.registerUser(user);
    }

你知道它有什么问题吗?

【问题讨论】:

    标签: java jackson


    【解决方案1】:

    你都试过了吗?

    @NotNull
    @Valid
    private final List<Books> books;
    

    编辑


    春季版:1.4.7.RELEASE

    书:

    public class Book {
    
        private String id;
        private String title;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
    }
    

    用户:

    public class User {
    
        @NotNull
        private int age;
    
        @NotNull
        @Valid
        private List<Book> books;
    
        public int getAge() {
            return age;
        }
    
         public void setAge(int age) {
            this.age = age;
         }
    
         public List<Book> getBooks() {
             return books;
         }
    
         public void setBooks(List<Book> books) {
             this.books = books;
         }
    
     }
    

    控制器:

    @PostMapping("/")
    public User post(@RequestBody @Valid User user) {
        return user;
    }
    

    测试:

    curl -s -XPOST http://localhost:8080/ -d '{"age": 13, "books": [{"id": "test", "title": "test"}]}' -H "Content-Type:application/json" | python -m json.tool
    
    {
      "age": 13,
      "books": [
        {
          "id": "test",
          "title": "test"
        }
       ]
    }
    
    curl -s -XPOST http://localhost:8080/ -d '{"age": 13}' -H "Content-Type:application/json" | python -m json.tool
    
    {
       "error": "Bad Request",
       "errors": [...]
    }
    

    显然,没有错……

    【讨论】:

    • 是的,结果一样 :(
    • 能否提供请求正文和您的 Books 类?
    • 这只是示例。 Books 类是一个具有少量字段(int、String)的普通类。我在原始帖子中添加了我的请求正文。
    • 实际上我是在询问客户端发送到后端的 http 请求正文。只是为了确保 json 格式与您的类匹配。
    • json 请求正确。我检查了很多次。当我发送包含所有字段的 json 时 - 工作正常。 - 没有非收集字段 - 工作正常(带有可清除消息的验证异常) - 没有 Set/List/Map - JsonParsingException(没有哪个字段为空的信息 - 我想知道它)。
    猜你喜欢
    • 2019-01-07
    • 1970-01-01
    • 2017-11-22
    • 2014-08-30
    • 1970-01-01
    • 2018-05-27
    • 2014-09-23
    • 2018-12-15
    • 1970-01-01
    相关资源
    最近更新 更多