ControllerAdvice配置不起作用

时间:2017-04-30 21:57:50

标签: spring

我在尝试获取控制器建议时遇到问题,下面是我的代码片段。我已经厌倦了stackoverflow上的大部分答案,但没有取得任何成功。如果有人能指导我,我将不胜感激。

```
@ControllerAdvice
public class PollCoreExceptionHandler {

    @ExceptionHandler(Exception.class)
    public @ResponseBody ResponseException handleIllegalArgumentServiceException(
            final Exception illegalArgumentServiceException) {
        System.out.println("Error handling called ");
        final ResponseException responseException = new ResponseException();
        responseException.setErrorMessage("Error handler called " + illegalArgumentServiceException.getMessage());
        return responseException;
    }

}
```

My controller 

```
@RequestMapping(value = "/login", method = RequestMethod.POST)
    public UserDTO loginUser(@RequestBody final UserDTO userDto) throws Exception {
        throw new Exception("error testing ");
    }
```

输出

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <title>Error 500 Server Error</title>
    </head>
    <body>
        <h2>HTTP ERROR 500</h2>
        <p>Problem accessing /ws/user/login. Reason:

            <pre>    Server Error</pre>
        </p>
        <h3>Caused by:</h3>
        <pre>javax.servlet.ServletException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.Exception: error testing 
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:564)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
    at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
    at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouK
</pre>
        <h3>Caused by:</h3>
        <pre>java.lang.Exception: error testing 
    at com.multi.enterprise.poll.core.controllers.UserController.loginUser(UserController.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet

我的应用程序启动时也会看到这一点。

2017-04-30 15:07:05,488 [main] INFO  annotation.ExceptionHandlerExceptionResolver:273 () - Detected @ExceptionHandler methods in controllerAdvisor
2017-04-30 15:07:05,488 [main] INFO  annotation.ExceptionHandlerExceptionResolver:273 () - Detected @ExceptionHandler methods in pollCoreExceptionHandler
2017-04-30 15:07:05,585 [main] INFO  servlet.DispatcherServlet:508 () - FrameworkServlet 'dispatcher': initialization completed in 1213 ms

0 个答案:

没有答案