java - 如何高效处理Spring中的错误: Could not resolve view with name 'forward:/oauth/error'

标签 java spring spring-mvc spring-security spring-boot

当 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/

相关文章:

mysql - 使用 queryForObject() 方法检索计数时,JDBCTemplate 中的参数索引超出范围(1 > 参数数量,即 0)

java - 从字符串中拆分地理坐标的正则表达式

java - 由于某种奇怪的原因,方法被调用两次?

java - Spring Boot - 重复 header

java - 检测到错误的 Node.js 版本

spring - 为什么 setDisallowedFields 为 id? -- Spring 宠物诊所示例

Java:Object.wait(long) 损坏

java - 如何从 pcollection 将多个值写入 redshift 表

java - spring-data-mongodb 可选查询参数

java - Hibernate 带有 @Transactional 的延迟初始化异常