【问题标题】:javax validation does not validate notNulljavax验证不验证notNull
【发布时间】:2019-10-07 07:54:38
【问题描述】:

我有一个使用 Spring Data for Couchbase 的 springBoot 2.1.9.RELEASE 应用程序

我有这个东西

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {

    @NotNull
    @JsonProperty("_location")
    private T location;

}

还有这个

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = { "id" })
@Builder
public class HTMDoc {

    @Id
    private String id;
    @NotNull
    @Field
    private Hostel hostel;

}

关于服务

public HTMDoc create(@Valid HTMDoc doc) {
    return repository.save(doc);
}

在测试中

service.create(new HTMDoc());

但是当我保存时,我得到了这个错误,而不是 hostel 字段中的验证 NotNull

 org.springframework.data.mapping.MappingException: An ID property is needed, but not found/could not be generated on this entity.

【问题讨论】:

  • id字段没有_@NotNull,为什么要验证呢?我会把它放在那里,我也会考虑应用 _@GeneratedValue (如果在 create 调用中没有提供 id ......)(嗯,如何在此处编写 java 注释以避免stackoverflow引擎将其识别为用户提及? )
  • 但是宿舍区可以
  • 是的,这意味着它将检查 HTMDoc.hostel 是否为空。但是您没有向验证引擎告知其他字段。
  • 您使用的是哪个 @NotNull 注释?显示完整的包装。

标签: java spring spring-boot spring-data-couchbase


【解决方案1】:

您需要在服务类上使用@org.springframework.validation.annotation.Validated 注释来启用验证。

@Validated
@Service
public class DocService {
  public HTMDoc create(@Valid HTMDoc doc) {
    return repository.save(doc);
  }
}

【讨论】:

    【解决方案2】:

    在id中加入如下注解,试试看:

    @Id
    @GeneratedValue(strategy = GenerationStrategy.UNIQUE)
    private String id;
    

    有关@GeneratedValue 注释的更多信息可以在这个很棒的答案中找到:Spring GeneratedValue annotation usage

    【讨论】:

      【解决方案3】:

      将注解放在getter上。

      可能私有字段不支持验证。

      【讨论】:

        【解决方案4】:
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public class Hostel<T> {
            @Id
            private Long id;
            @NotNull
            @JsonProperty("_location")
            private T location;
        
        }
        

        在模型类中必须对任何属性使用@Id。

        【讨论】:

          【解决方案5】:

          你需要在你的类中使用@Validated注解来通知spring boot这些类应该通过javax验证来验证。

          @Validated
          @Component
          public class Phone {
           //Your logic
          }
          

          【讨论】:

            【解决方案6】:

            请在您的 id 字段中添加以下语法

            @Data
            @AllArgsConstructor
            @NoArgsConstructor
            public class Hostel<T> {
                @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                @Column(name = "id", nullable = false, updatable = false)
                private Long id;
            
                @NotNull
                @JsonProperty("_location")
                private T location;
            
            }
            

            也在你的服务类中使用验证注解。

            @Validated
            @Service
            public class DocService {
              public HTMDoc create(@Valid HTMDoc doc) {
                return repository.save(doc);
              }
            }
            

            【讨论】:

              猜你喜欢
              • 2019-01-07
              • 1970-01-01
              • 2011-05-17
              • 1970-01-01
              • 2017-11-22
              • 2014-09-23
              • 1970-01-01
              • 2022-11-27
              • 1970-01-01
              相关资源
              最近更新 更多