请求对象错误的Spring验证无法在@ControllerAdvice中找到消息

时间:2015-03-09 14:08:57

标签: java spring rest spring-mvc spring-boot

所有其他错误通过ControllerAdvice正常工作,除了在幕后从验证器抛出的错误,控制器建议捕获异常但无法从@autowired消息源中找到消息。这是因为验证器调用错误处理的方式?在eclipse中作为Spring Boot App运行。 Spring Boot版本1.2.1.RELEASE。还使用spring cloud config。

  @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

@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);
    }
}

验证类

public class GetPolicyDetails {


    @NotNull
    @Size(min=7, max=10)
    @Pattern(regexp = "^[0-9]*$")
    private String policyNbr;

    public String getPolicyNbr() {
        return policyNbr;
    }

    public void setPolicyNbr(String policyNbr) {
        this.policyNbr = policyNbr;
    }
}

应用程序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

1 个答案:

答案 0 :(得分:0)

发现@ConditionalOnMissingBean(MessageSource.class)正在引导过程中使用,但是使用云配置时,从未加载此资源,默认的MessageSource在没有我的消息的情况下使用。两种解决方法

将其添加到Application类以覆盖默认值。

 @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