相关疑难解决方法(0)

Spring MVC Spring安全性和错误处理

我正在使用ResponseEntityExceptionHandler来全局处理错误并且几乎正常工作,除了我想用spring处理错误的请求.通过任何逻辑覆盖handleNoSuchRequestHandlingMethod应该处理这个,但是处理总是得到

HTTP状态404 -

类型状态报告

信息

description请求的资源不可用.

Apache Tomcat/7.0.37

我在控制台中启用debuging时得到了这个:

警告:org.springframework.web.servlet.PageNotFound - 找不到带URI的HTTP请求的映射

只是为了通过处理澄清我的意思是我正在返回JSON.

任何想法如何处理这个?

rest spring spring-security

14
推荐指数
1
解决办法
9333
查看次数

从404上的RESTful Spring API返回错误JSON(不明确的@ExceptionHandler)

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

但是,当我发出导致404的请求而不是看到错误JSON时,我得到以下输出:

<!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>
Run Code Online (Sandbox Code Playgroud)

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

<init-param>
    <param-name>throwExceptionIfNoHandlerFound</param-name>
    <param-value>true</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)

并使用类似于此的全局建议:

@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"); …
Run Code Online (Sandbox Code Playgroud)

rest spring exception-handling http-status-code-404

3
推荐指数
1
解决办法
5676
查看次数