我对spring验证有一个奇怪的问题,我有一个类是object value,我在它的字段上添加了一些验证注解。我已经编写了一些单元测试来证明注解工作正常。但是当我发送空值时,当涉及到测试控制器时,验证不起作用,让数据进入。这是我的价值目标:
@Data
@ToString
public class NameEN {
@NotNull(message = "Name can not be null!")
@Size(min=4, message = "Name length should be more than 3 characters!")
@Pattern(regexp = "[a-zA-Z]+")
private String nameEn;
// @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public NameEN(String nameEn) {
this.nameEn = nameEn;
}
这里是我使用这个值对象的地方:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateCategory {
@Valid
private String name_en;
@Valid
private String name;
private int parent;
private String image;
private String kind;
private int priority;
private boolean visibility;
public Category asCategory(){
return new Category(new NameEN(name_en),new Name(name),parent,image,kind,priority,visibility);
}
}
这是我的dto:
@Data
@Entity
@Table(name = "category")
class CategoryDTO {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Valid
private String name_en;
@Valid
private String name;
private int parent;
private String image;
private String kind;
private int priority;
private boolean visibility;
private boolean deleted;
@CreationTimestamp
private Timestamp cdt;
@UpdateTimestamp
private Timestamp udt;
public CategoryDTO() {
}
public CategoryDTO(Long id, @Valid String name_en, @Valid String name, int parent, String image, String kind, int priority, boolean visibility, boolean deleted, Timestamp cdt, Timestamp udt) {
this.id = id;
this.name_en = name_en;
this.name = name;
this.parent = parent;
this.image = image;
this.kind = kind;
this.priority = priority;
this.visibility = visibility;
this.deleted = deleted;
this.cdt = cdt;
this.udt = udt;
}
public static CategoryDTO from(Category category){
return new CategoryDTO(category.getId(),category.getName_en().asRaw(),category.getName().asRaw(),category.getParent(),category.getImage(),category.getKind(),category.getPriority(),category.isVisibility(),category.isDeleted(),category.getCdt(),category.getUdt());
}
Category asCategory(){
return new Category(new NameEN(name_en),new Name(name),parent,image,kind,priority,visibility);
}
Category asResponse(){
return new Category(id,new NameEN(name_en),new Name(name),parent,image,kind,priority,visibility);
}
}
这是控制器:
@PostMapping(value = "create")
public ResponseEntity<?> createCategory(@Valid @RequestBody CreateCategory createCategory) {
Category category = createCategory.asCategory();
return ResponseEntity.status(HttpStatus.CREATED).body(categoryRepository.save(category));
}
最后,这是服务:
public Category save(Category category) {
CategoryDTO dto= CategoryDTO.from(category);
dto= categoryJpaRepository.saveAndFlush(dto);
return dto.asResponse();
}
此单元测试通过:
@Test
public void whenNullNameEn_ThenOneConstraintViolation(){
validator = Validation.buildDefaultValidatorFactory().getValidator();
NameEN nameEN = new NameEN(null);
validator.validate(nameEN).stream().forEach(violation -> System.out.println(violation.getMessage()));
Set<ConstraintViolation<NameEN>> violations = validator.validate(nameEN);
assertThat(violations.size()).isEqualTo(1);
}
但是当我在postman上发送这个json时,并不能避免创建类别:
{
"name_en":null,
"name": null,
"parent": 1,
"image": "ss",
"kind" :2,
"priority": 1,
"visobility": true
}
Postman 返回:
{
"id": 51,
"name_en": {
"nameEn": null
},
"name": {
"name": null
},
"parent": 1,
"image": "ss",
"kind": "2",
"priority": 1,
"visibility": false,
"deleted": false,
"cdt": "2020-12-07T21:12:33.086+00:00",
"udt": "2020-12-07T21:12:33.086+00:00"
}
我的问题在哪里?我检查了所有的东西,我不能理解这个问题!
暂无答案!
目前还没有任何答案,快来回答吧!