我正在尝试自定义从服务中的@NotNull属性获得的验证错误:
{
"code": 26,
"message": "Validation Error: :NotEmpty",
"data": [
{
"type": "NotEmpty",
"property": "",
"value": null,
"inputType": "body",
"attributes": null
}
],
"source": "XXXX",
"type": "ValidationException",
"guid": "965ee91a-99a1-4ae5-b721-6415a80dad23"
}
尝试过像这样重写LocalValidatorFactoryBean和MessageSource:
@Bean
public LocalValidatorFactoryBean getValidator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource
= new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
并在我的类路径中创建一个 messages.properties 文件,并在其中包含我的消息。
我的服务:
@POST
@Path("")
@Consumes({"application/x-www-form-urlencoded"})
TokenDTO login(@ApiParam(value = "The id of the channel used", required = true ) @HeaderParam("channel") @NotEmpty(message = "email.notempty") String channel) throws Exception;
并且,API调用中的消息仍然不变。
有人可以帮忙吗?