1 回答
TA贡献1851条经验 获得超3个赞
我会使用 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;
}
}
添加回答
举报