내 프로젝트에서 내 컨트롤러에 예외 처리기 메서드를 추가 할 때 작동하지 않습니다. 그러나이 코드를 Spring 버전이 동일한 데모 프로젝트로 옮기면 missingParamHandler 메서드가 잘 작동합니다. 누구든지이 문제를 처리하도록 도울 수 있습니까?@ExceptionHandler가 작동하지 않는 이유를 찾는 방법은 무엇입니까?
@Controller
@RequestMapping("/order")
public class OrderControlle{
@ExceptionHandler(Exception.class)
public @ResponseBody ClientResult missingParamterHandler(Exception exception) {
/*inspect and exception and obtain meaningful message*/
ClientResult clientResult = new ClientResult();
clientResult.getBstatus().setCode(BstatusCode.PARAM_ERR);
return clientResult;
}
}
내가 디버그를 시도하고 봄의 DispatcherServlet.java에서 발견, matchingBeans.isEmpty()가 true를 돌려, 이유 @ExceptionHandler (Exception.class) 내 프로젝트에서 작동하지입니까?
private void initHandlerExceptionResolvers(ApplicationContext context) {
this.handlerExceptionResolvers = null;
if (this.detectAllHandlerExceptionResolvers) {
// Find all HandlerExceptionResolvers in the ApplicationContext, including ancestor contexts.
Map<String, HandlerExceptionResolver> matchingBeans = BeanFactoryUtils
.beansOfTypeIncludingAncestors(context, HandlerExceptionResolver.class, true, false);
if (!matchingBeans.isEmpty()) {
this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values());
// We keep HandlerExceptionResolvers in sorted order.
OrderComparator.sort(this.handlerExceptionResolvers);
}
}
.....
'context'에'HandlerExceptionResolver' 빈이 정의되어 있습니까? –
잘 모르겠습니다. 어디에서 확인할 수 있습니까? @ExceptionHandler (Exception.class) annotaion으로 충분하지 않습니다? – yanyu