首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ExceptionHandler的ExceptionHandler404返回错误RESTful Spring API (歧义@ExceptionHandler.)

从ExceptionHandler的ExceptionHandler404返回错误RESTful Spring API (歧义@ExceptionHandler.)
EN

Stack Overflow用户
提问于 2015-06-10 05:34:07
回答 2查看 6.5K关注 0票数 3

我正在使用Spring (4.0.4) MVC来构建一个RESTful应用程序接口。我需要返回一个ResponseEntity,其中包含一个错误的JSON表示,http状态为404。

但是,当我发出一个产生404的请求时,我没有看到错误JSON,而是得到了以下输出:

代码语言:javascript
复制
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /project/api/resource/100 was not found on this server.</p>
<p>Additionally, a 503 Service Temporarily Unavailable
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>

this stackoverflow post中,我发现可以通过在我的web.xml中包含以下参数来防止发送这个错误页面来代替错误

代码语言:javascript
复制
<init-param>
    <param-name>throwExceptionIfNoHandlerFound</param-name>
    <param-value>true</param-value>
</init-param>

并使用类似如下的全局建议:

代码语言:javascript
复制
@ControllerAdvice
public class MyExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ResponseBody
    public ResponseEntity<ErrorResponse> requestHandlingNoHandlerFound(HttpServletRequest req,
            NoHandlerFoundException ex) {

        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setText("error response");
        return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.BAD_REQUEST);
    }
}

然而,当我添加这个MyExceptionHandler类时,我得到了以下异常:

代码语言:javascript
复制
Caused by: java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.servlet.NoHandlerFoundException]: {public org.springframework.http.ResponseEntity com.rest.MyExceptionHandler.requestHandlingNoHandlerFound

问题是它不知道是调用我的自定义ExceptionHandler还是调用web.servlet中定义的call。

如何解决此问题,以便能够使用Spring从API返回JSON?

EN

回答 2

Stack Overflow用户

发布于 2015-06-10 09:44:12

这是我认为正在发生的事情:

您的ExceptionHandler类扩展了ResponseEntityExceptionHandler,它有一个handleNoHandlerFoundException方法(请参阅source code中this文件末尾的部分),您还可以使用@ExceptionHandler注释方法来处理相同的异常。

代码语言:javascript
复制
@Override
ResponseEntity handleNoHandlerFoundException(NoHandlerFoundException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request)

我认为在你的代码中同时包含这两个是导致这个问题的原因。

因此,要么:

ExceptionHandler覆盖该方法(且不使用@ExceptionHandler注释方法)

  • 不扩展ResponseEntityExceptionHandler。请改用@ExceptionHandler注释的方法。

我认为这两个选项中的任何一个都应该有效。

票数 6
EN

Stack Overflow用户

发布于 2016-01-05 13:13:56

尝尝这个。

代码语言:javascript
复制
package x.y.z;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(value=HttpStatus.NOT_FOUND)
    @ResponseBody
    public ResponseEntity<String> handle404() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        String json = "{\"error\":\"Resource not found.\"}";

        return new ResponseEntity<String>(json, headers, HttpStatus.NOT_FOUND);
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30743230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档