javax验证不验证notNull
javax validation does not validate notNull
我有一个 springBoot 2.1。9.RELEASE 应用程序使用 Spring Couchbase 数据
我有这个对象
@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());
但是当我保存时,我得到了这个错误,而不是旅馆字段中的验证 NotNull
org.springframework.data.mapping.MappingException: An ID property is needed, but not found/could not be generated on this entity.
给id添加如下注解试试看:
@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
有关 @GeneratedValue
注释的更多信息可以在这个很好的答案中找到:
将注释放在 getter 上。
可能私有字段不支持验证。
您需要在您的服务 class 上使用 @org.springframework.validation.annotation.Validated
注释来启用验证。
@Validated
@Service
public class DocService {
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {
@Id
private Long id;
@NotNull
@JsonProperty("_location")
private T location;
}
在模型 类 中必须在任何 属性 上使用 @Id。
请在您的 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;
}
也在您的服务中使用验证注释 class。
@Validated
@Service
public class DocService {
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
}
您需要在 类 中使用 @Validated 注释来通知 spring 启动这些 类 应该由 javax 验证来验证。
@Validated
@Component
public class Phone {
//Your logic
}
我有一个 springBoot 2.1。9.RELEASE 应用程序使用 Spring Couchbase 数据
我有这个对象
@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());
但是当我保存时,我得到了这个错误,而不是旅馆字段中的验证 NotNull
org.springframework.data.mapping.MappingException: An ID property is needed, but not found/could not be generated on this entity.
给id添加如下注解试试看:
@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
有关 @GeneratedValue
注释的更多信息可以在这个很好的答案中找到:
将注释放在 getter 上。
可能私有字段不支持验证。
您需要在您的服务 class 上使用 @org.springframework.validation.annotation.Validated
注释来启用验证。
@Validated
@Service
public class DocService {
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {
@Id
private Long id;
@NotNull
@JsonProperty("_location")
private T location;
}
在模型 类 中必须在任何 属性 上使用 @Id。
请在您的 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;
}
也在您的服务中使用验证注释 class。
@Validated
@Service
public class DocService {
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
}
您需要在 类 中使用 @Validated 注释来通知 spring 启动这些 类 应该由 javax 验证来验证。
@Validated
@Component
public class Phone {
//Your logic
}