Java 请求对象错误的Spring验证在@ControllerAdvice中找不到消息
通过ControllerAdvice,所有其他错误都可以正常工作,除了验证程序在幕后抛出的错误,控制器建议捕获异常,但无法找到来自@autowired message source的消息。这是因为验证程序调用错误处理的方式吗?在eclipse中作为Spring启动应用程序运行。Spring Boot版本1.2.1.0发布。也可以使用SpringCloud配置Java 请求对象错误的Spring验证在@ControllerAdvice中找不到消息,java,spring,rest,spring-mvc,spring-boot,Java,Spring,Rest,Spring Mvc,Spring Boot,通过ControllerAdvice,所有其他错误都可以正常工作,除了验证程序在幕后抛出的错误,控制器建议捕获异常,但无法找到来自@autowired message source的消息。这是因为验证程序调用错误处理的方式吗?在eclipse中作为Spring启动应用程序运行。Spring Boot版本1.2.1.0发布。也可以使用SpringCloud配置 @RestController public class AutoController { private static fi
@RestController
public class AutoController {
private static final Logger logger = LoggerFactory.getLogger(AutoController.class);
@Autowired
private AutoFacade autoFacade;
@RequestMapping(value = "/policies/policy/{policyNbr}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public AutoPolicy getPolicyDetails(@Valid GetPolicyDetails reqModelObj) {
AutoPolicy policyDetails = autoFacade.getPolicyDetails(reqModelObj.getPolicyNbr());
return policyDetails;
}
}
控制器建议
@ControllerAdvice
public class RestExceptionProcessor {
private static final Logger logger = LoggerFactory.getLogger(RestExceptionProcessor.class);
@Autowired
private MessageSource messageSource;
@ExceptionHandler({APIServerErrorException.class})
@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public APIStatus serverError(APIServerErrorException ex) {
int statusCd = Integer.parseInt(HttpStatus.INTERNAL_SERVER_ERROR.toString());
String statusMessage = messageSource.getMessage("internal.error.status.msg.500", null, Locale.US);
String errorURL = null;
List<APIError> errorInfoList = ex.getApiErrorList();
if (errorInfoList == null || errorInfoList.isEmpty()) {
int errorCode = (ex.getCode() > 0 ? ex.getCode() : 500000);
String errorType = messageSource.getMessage("internal.error.error.type.500", null, Locale.US);
APIError apiError = new APIError(errorCode, errorType, ex.getMessage());
errorInfoList = new ArrayList<APIError>();
errorInfoList.add(apiError);
}
return new APIStatus(statusCd, errorURL, statusMessage, errorInfoList);
}
@ExceptionHandler({APINotFoundException.class})
@ResponseStatus(value=HttpStatus.NOT_FOUND)
@ResponseBody
public APIStatus notFoundError(APINotFoundException ex) {
int statusCd = Integer.parseInt(HttpStatus.NOT_FOUND.toString());
String statusMessage = messageSource.getMessage("not.found.status.msg.404", null, Locale.US);
String errorURL = null;
List<APIError> errorInfoList = ex.getApiErrorList();
if (errorInfoList == null || errorInfoList.isEmpty()) {
int errorCode = (ex.getCode() > 0 ? ex.getCode() : 404000);
String errorType = messageSource.getMessage("not.found.error.type.404", null, Locale.US);
APIError apiError = new APIError(errorCode, errorType, ex.getMessage());
errorInfoList = new ArrayList<APIError>();
errorInfoList.add(apiError);
}
return new APIStatus(statusCd, errorURL, statusMessage, errorInfoList);
}
@ExceptionHandler({APIClientErrorException.class})
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ResponseBody
public APIStatus clientError(APIClientErrorException ex) {
int statusCd = Integer.parseInt(HttpStatus.BAD_REQUEST.toString());
String statusMessage = messageSource.getMessage("bad.request.status.msg.400", null, Locale.US);
String errorURL = null;
List<APIError> errorInfoList = ex.getApiErrorList();
if (errorInfoList == null || errorInfoList.isEmpty()) {
int errorCode = (ex.getCode() > 0 ? ex.getCode() : 400000);
String errorType = messageSource.getMessage("bad.request.error.type.400", null, Locale.US);
APIError apiError = new APIError(errorCode, errorType, ex.getMessage());
errorInfoList = new ArrayList<APIError>();
errorInfoList.add(apiError);
}
return new APIStatus(statusCd, errorURL, statusMessage, errorInfoList);
}
@ExceptionHandler({MethodArgumentNotValidException.class})
@ResponseStatus(value=HttpStatus.UNPROCESSABLE_ENTITY)
@ResponseBody
public APIStatus validationError(MethodArgumentNotValidException ex) {
int statusCd = Integer.parseInt(HttpStatus.UNPROCESSABLE_ENTITY.toString());
String statusMessage = messageSource.getMessage("unprocessable.entity.status.msg.422", null, Locale.US);
String errorURL = null;
int errorCode = 422000;
String errorType = messageSource.getMessage("unprocessable.entity.error.type.422", null, Locale.US);
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
List<com.fbitn.api.errors.FieldError> apiFieldErrors = new ArrayList<com.fbitn.api.errors.FieldError>();
for (FieldError fieldError: fieldErrors) {
String errorMessage = messageSource.getMessage(fieldError, Locale.US);
com.fbitn.api.errors.FieldError apiFieldError = new com.fbitn.api.errors.FieldError(fieldError.getField(), errorMessage);
apiFieldErrors.add(apiFieldError);
}
APIError apiError = new APIError(errorCode, errorType, "Validation Error", apiFieldErrors);
List<APIError> errorInfoList = new ArrayList<APIError>();
errorInfoList.add(apiError);
return new APIStatus(statusCd, errorURL, statusMessage, errorInfoList);
}
@ExceptionHandler({Throwable.class})
@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public APIStatus unhandledError(Throwable ex) {
int statusCd = Integer.parseInt(HttpStatus.INTERNAL_SERVER_ERROR.toString());
String statusMessage = messageSource.getMessage("test", null, Locale.US);
String errorURL = null;
int errorCode = 500000;
String errorType = messageSource.getMessage("internal.error.error.type.500", null, Locale.US);
APIError apiError = new APIError(errorCode, errorType, ex.getMessage());
List<APIError> errorInfoList = new ArrayList<APIError>();
errorInfoList.add(apiError);
logger.error("Unhandled Error", ex);
return new APIStatus(statusCd, errorURL, statusMessage, errorInfoList);
}
}
应用程序yml
spring:
application:
name: auto
messages:
basename: messages
encoding: UTF-8
cacheSeconds: -1
endpoints:
shutdown:
enabled: true
default:
locale:en_US
项目结构
src/main/resources
messages_en.properties
财产
internal.error.status.msg.500=Internal Error
test=test
internal.error.errror.type.500=Internal Error
bad.request.status.msg.400 = Bad Request
bad.request.error.type.400 = Bad Request
not.found.status.msg.404 = Not Found
not.found.error.type.404 = Not Found
unprocessable.entity.status.msg.422 = Unprocessable Entity
unprocessable.entity.error.type.422 = Unprocessable Entity
发现@ConditionalOnMissingBeanMessageSource.class正在引导过程中使用,但使用cloud config时,从未加载此资源,使用默认MessageSource时未加载我的消息。有两种方法可以解决这个问题 将此添加到应用程序类以覆盖默认值
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames( "classpath:messages" );
// if true, the key of the message will be displayed if the key is not
// found, instead of throwing a NoSuchMessageException
messageSource.setUseCodeAsDefaultMessage( true );
messageSource.setDefaultEncoding( "UTF-8" );
// # -1 : never reload, 0 always reload
messageSource.setCacheSeconds( 0 );
return messageSource;
}
或者添加一个工厂来订购meta-inf中的加载首选项
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration