如何实现 Spring ErrorController 而 Sonar 不报错?
How to implement a Spring ErrorController where Sonar does not complain?
我实现了自定义错误控制器来向用户显示自定义错误视图。我遵循了一个教程(再也找不到了),所以我的控制器看起来像这样:
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
Integer statusCode = Integer.valueOf(status.toString());
if (statusCode == HttpStatus.FORBIDDEN.value()) {
return "error/403";
} else if (statusCode == HttpStatus.NOT_FOUND.value()) {
return "error/404";
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return "error/500";
}
}
return "error/default";
}
@Override
public String getErrorPath() {
return "/error";
}
}
到目前为止一切顺利,但自 2019 年 5 月 17 日起,SonarQube 抱怨 @RequestMapping
没有方法。所以我添加了我正在使用的 4 种方法:
@RequestMapping(value = "/error", method = { RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT,
RequestMethod.DELETE })
但是Sonar现在抱怨我的方法太多了。那么实现符合this Sonar rule的自定义ErrorController的正确方法是什么?
由于这是您的 "default error page" 路由,由于重定向,它始终是 GET,因此您可以安全地更改为 @GetMapping
。
按照这个写在web.xml
<display-name>App Name </display-name>
<error-page>
<error-code>500</error-code>
<location>/error500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
Spring MVC: How to return custom 404 errorpages?
您可以使用 @SuppressWarnings("squid:S3752")
抑制特定的 Sonar 规则,如下所示:
@Controller
public class CustomErrorController implements ErrorController {
@SuppressWarnings("squid:S3752")
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
...
}
最好使用 @SuppressWarnings
禁用此特定规则,而不是像评论中建议的那样,使用 //NOSONAR
完全禁用 Sonar(参见 this question) .
不幸的是,Antoniosss 的 (更改为 @GetMapping
)不正确。
Spring 不会将重定向发送回浏览器,但会在内部重定向到您的 CustomErrorController
- 重定向的请求将使用与导致错误的原始请求相同的方法。
如果将 handleError()
方法限制为仅处理 GET
方法,如果原始请求是 POST
.
,则客户端将收到 405 Method not allowed
响应
我实现了自定义错误控制器来向用户显示自定义错误视图。我遵循了一个教程(再也找不到了),所以我的控制器看起来像这样:
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
Integer statusCode = Integer.valueOf(status.toString());
if (statusCode == HttpStatus.FORBIDDEN.value()) {
return "error/403";
} else if (statusCode == HttpStatus.NOT_FOUND.value()) {
return "error/404";
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return "error/500";
}
}
return "error/default";
}
@Override
public String getErrorPath() {
return "/error";
}
}
到目前为止一切顺利,但自 2019 年 5 月 17 日起,SonarQube 抱怨 @RequestMapping
没有方法。所以我添加了我正在使用的 4 种方法:
@RequestMapping(value = "/error", method = { RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT,
RequestMethod.DELETE })
但是Sonar现在抱怨我的方法太多了。那么实现符合this Sonar rule的自定义ErrorController的正确方法是什么?
由于这是您的 "default error page" 路由,由于重定向,它始终是 GET,因此您可以安全地更改为 @GetMapping
。
按照这个写在web.xml
<display-name>App Name </display-name>
<error-page>
<error-code>500</error-code>
<location>/error500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
Spring MVC: How to return custom 404 errorpages?
您可以使用 @SuppressWarnings("squid:S3752")
抑制特定的 Sonar 规则,如下所示:
@Controller
public class CustomErrorController implements ErrorController {
@SuppressWarnings("squid:S3752")
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
...
}
最好使用 @SuppressWarnings
禁用此特定规则,而不是像评论中建议的那样,使用 //NOSONAR
完全禁用 Sonar(参见 this question) .
@GetMapping
)不正确。
Spring 不会将重定向发送回浏览器,但会在内部重定向到您的 CustomErrorController
- 重定向的请求将使用与导致错误的原始请求相同的方法。
如果将 handleError()
方法限制为仅处理 GET
方法,如果原始请求是 POST
.
405 Method not allowed
响应