我收到此错误消息:
对于类型为'org.springframework.context.MessageSource'的参数0没有合适的解析器
这是相关代码:
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ExceptionControllerAdvice {
@ExceptionHandler({DocumentAlreadyExistsException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public cat.gencat.ctti.canigo.arch.web.rs.model.Error handleException(MessageSource messageSource, DocumentAlreadyExistsException e) {
cat.gencat.ctti.canigo.arch.web.rs.model.Error error = new cat.gencat.ctti.canigo.arch.web.rs.model.Error();
error.setCode(HttpStatus.BAD_REQUEST.value());
error.setMessage(messageSource.getMessage(e.getLocalizedMessage(), null, null));
return error;
}
}
另一方面,我创建了这个bean:
@Configuration
public class WebServicesConfiguration {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("messages/exceptions/document");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
有什么想法吗?
答案 0 :(得分:1)
我将使用bean,而不是在方法签名上使用,而是在类(构造函数或setter方法)上使用
:@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ExceptionControllerAdvice {
private MessageSource messageSource;
public ExceptionControllerAdvice(MessageSource messageSource) {
this.messageSource = messageSource;
}
@ExceptionHandler({DocumentAlreadyExistsException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public cat.gencat.ctti.canigo.arch.web.rs.model.Error handleException( DocumentAlreadyExistsException e) {
cat.gencat.ctti.canigo.arch.web.rs.model.Error error = new cat.gencat.ctti.canigo.arch.web.rs.model.Error();
error.setCode(HttpStatus.BAD_REQUEST.value());
error.setMessage(messageSource.getMessage(e.getLocalizedMessage(), null, null));
return error;
}
}