2015-01-21 3 views
1

내 프로젝트에서 내 컨트롤러에 예외 처리기 메서드를 추가 할 때 작동하지 않습니다. 그러나이 코드를 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); 
     } 
    } 
    ..... 
+1

'context'에'HandlerExceptionResolver' 빈이 정의되어 있습니까? –

+0

잘 모르겠습니다. 어디에서 확인할 수 있습니까? @ExceptionHandler (Exception.class) annotaion으로 충분하지 않습니다? – yanyu

답변

1

명시 적 <bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" /> 또는 <mvc:annotation-driven />이-context.xml에 app 해당 내 문제를 해결하기 위해 추가 할 수 있습니다.

위의 행이 없으면 Spring은 내 프로젝트에서 아래와 같이 ExceptionResolvers를 추가합니다.

org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\ 
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\ 
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver 
관련 문제