0

我在我的 SpringBoot 项目中使用http://immutables.github.io 。我添加了 Javax 验证注释以验证类,但它不起作用。我试图这样做:

@Value.Immutable
@JsonDeserialize(builder = UserSignUpRequest.Builder.class)
@JsonSerialize(as = UserSignUpRequest.class)
@JsonIgnoreProperties(ignoreUnknown = true)
@ImmutableJsonModel
interface AbstractUserSignUpRequest {
Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();

@Value.Parameter
@Pattern(regexp = RegexPatterns.PHONE_REGEX)
String phone();

@Value.Parameter
@NotBlank
String username();

@Value.Parameter
Optional<DeviceRegistrationRequest> device();

@Value.Check
default void check() {
Set<ConstraintViolation<AbstractUserSignUpRequest>> violations = VALIDATOR.validate(this);
if (!violations.isEmpty()) {
  throw new ConstraintViolationException(violations);
  }
 }
}

在这样的控制器主体中:

@PostMapping(
  value = "/sign-up",
  consumes = MediaType.APPLICATION_JSON_VALUE,
  produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<ResponseEntity<UserCreatedResponse>> signUp(
  @RequestBody Mono<@Valid UserSignUpRequest> request) {

return request
    .flatMap(authenticationService::signUp)
    .map(user -> ResponseEntity.status(CREATED).body(user));
}

我的代码不起作用,我无法用这种方式验证类。应该如何在不可变类中实现 javax 验证注解?

4

0 回答 0