【发布时间】:2019-06-19 22:04:33
【问题描述】:
我有一个看起来像这样的ConstraintViolationException 处理程序类:
@Produces
@Singleton
@Requires(classes = {ConstraintViolationException.class, ExceptionHandler.class})
public class ConstraintsViolationsExceptionHandler
implements ExceptionHandler<ConstraintViolationException, HttpResponse> {
@Override
public HttpResponse
handle(HttpRequest request, ConstraintViolationException exception) {
return HttpResponse
.status(HttpStatus.FORBIDDEN)
.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("UTF-8")
.body(new SignUpPhoneNumberErrorResponse<>(400,
"Wrong data used",
new ArrayList<>(exception.getConstraintViolations())));
}
}
SignUpPhoneNumberErrorResponse 是我的错误处理 POJO,它被序列化为 JSON 非常好。
我的控制器如下所示:
@Controller(PhoneAuthAndLoginConstants.CONTROLLER_BASE_PATH)
@Validated
public class UserPhoneNumberRegistrationAndLoginController {
@Inject
MongoDbUserSignUpPhoneNumberDAO mongoDbUserSignUpPhoneNumberDAO;
@Post(uri = PhoneAuthAndLoginConstants.CONTROLLER_SIGN_UP_PATH,
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON)
public Single<ResponseDataEncapsulate>
signUpForPhoneVerification(@Valid @Body UserSignUpPhoneNumberEntity phoneNumber) {
return mongoDbUserSignUpPhoneNumberDAO.sendVerification(phoneNumber);
}
@Post(uri = PhoneAuthAndLoginConstants.CONTROLLER_SIGN_UP_PATH
+
PhoneAuthAndLoginConstants.CONTROLLER_SIGN_UP_VERIFICATION_CODE_PARAM,
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON)
public Single<ResponseDataEncapsulate>
sendUserSignUpConfirmation(@Valid @Body UserAccountStateSignUpEntity verificationData,
HttpHeaders httpHeaders) {
return mongoDbUserSignUpPhoneNumberDAO.signUp(verificationData);
}
}
UserAccountStateSignUpEntity 的 POJO 如下所示:
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserAccountStateSignUpEntity implements UserSignUpEntity {
@NotNull @NotBlank @Size(min = 5, max = 13) private String phoneNumber;
@NotNull @NotBlank @Size(min = 7, max = 7) private String verificationCode;
@JsonIgnore private Boolean verifiedAccount = Boolean.FALSE;
public UserAccountStateSignUpEntity(String phoneNumber, String verificationCode) {
this.phoneNumber = phoneNumber;
this.verificationCode = verificationCode;
this.verifiedAccount = Boolean.TRUE;
}
@Override
public Map<String, Object> makePhoneEntityMapForMongo() {
HashMap<String, Object> returnMap = new HashMap<String, Object>() {{
put("phoneNumber", phoneNumber);
put("verificationCode", verificationCode);
put("verifiedAccount", verifiedAccount);
}};
return Collections.unmodifiableMap(returnMap);
}
}
我发送这样的请求负载:
{
"phoneNumber" : "91-123456789",
"verificationCode" : "18887"
}
这应该会触发ConstraintViolationException 并且我的处理程序代码应该执行并且我应该得到一个 HTTP Forbidden。但相反,我收到了默认的 HTTP Bad Request 错误消息。
为什么我的处理程序没有被执行? 可以做些什么来让它执行?
我使用 Micronaut 1.1.3 作为 Web 框架,使用 Hibernate Validator 作为 javax.validation 实现。
【问题讨论】:
-
你是使用
io.micronaut.configuration:micronaut-hibernate-validator还是原来的hebernate依赖?看看docs.micronaut.io/1.2.0.RC1/guide/index.html#datavalidation -
@tsarenkotxt 我尝试对我的 POJO 使用
@Introspected注释,但仍然没有结果。在我的情况下,只有@Introspected注释才有意义。 -
@tsarenkotxt 我正在使用 Micronaut v1.1.3,您提供的指南包含 v1.2.0.RC1 的
@Inteospected注释,而 v1.1.3 中没有。 -
是的,你对版本是正确的,我的意思是你的类路径中的依赖项,以确保你添加
io.micronaut.configuration:micronaut-hibernate-validator -
@tsarenkotxt 我已经有了所需的依赖项,并且在日志中可见
标签: java bean-validation hibernate-validator micronaut