当 oauth2 client_id 无效时,我的其余 api 会创建此错误作为响应。
{"timestamp":"2016-10-06T05:26:31.367+0000","status":500,"error":"Internal Server Error","exception":"org.springframework.security.oauth2.provider.NoSuchClientException","message":"No client with requested id: backoffice","path":"/oauth/authorize"}
我的日志中有服务器端:
Looking up handler method for path /oauth/authorize
Did not find handler method for [/oauth/authorize]
Handling ClientRegistrationException error: No client with requested id: backoffice
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Could not resolve view with name 'forward:/oauth/error' in servlet with name 'dispatcherServlet'] with root cause
javax.servlet.ServletException: Could not resolve view with name 'forward:/oauth/error' in servlet with name 'dispatcherServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1237)
...
at java.lang.Thread.run(Thread.java:745)
问题:防止服务器向他提供类异常并处理此错误的最佳方法是什么?
我尝试遵循 @ControllerAdvice
的建议,如 article 中所述。但代码从未通过断点:
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(NoSuchClientException.class)
public String handleNoSuchClientException(HttpServletRequest request, Exception ex){
logger.info("NoSuchClientException Occured:: URL="+request.getRequestURL());
return "no_such_client_error";
}
}
最佳答案
您需要将注释@ResponseBody
添加到每个ControllerAdvice
方法。
关于java - 如何高效处理Spring中的错误: Could not resolve view with name 'forward:/oauth/error' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39888300/