Spring Boot - 处理 JSON 或 HTML 的错误控制器 [英] Spring Boot - Error Controller to handle either JSON or HTML

查看:28
本文介绍了Spring Boot - 处理 JSON 或 HTML 的错误控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 应用程序.

I have a spring boot application.

我有一个自定义错误控制器,它被映射到使用 ErrorPage 映射.映射主要基于 HTTP 状态代码,通常只是适当地呈现 HTML 视图.

I have a custom error controller, that is mapped to using ErrorPage mappings. The mappings are largely based on HTTP Status codes, and normally just render a HTML view appropriately.

例如,我的映射:

@Configuration
class ErrorConfiguration implements EmbeddedServletContainerCustomizer {

  @Override public void customize( ConfigurableEmbeddedServletContainer container ) {
      container.addErrorPages( new ErrorPage( HttpStatus.NOT_FOUND, "/error/404.html" ) )
  }

还有我的错误控制器:

@Controller
@RequestMapping
public class ErrorController {

    @RequestMapping( value = "/error/404.html" )
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public String pageNotFound( HttpServletRequest request ) {
        "errors/404"
    }

这很好用 - 如果我只是输入一个随机的不存在的 URL,那么它会呈现 404 页面.

This works fine - If I just enter a random non-existent URL then it renders the 404 page.

现在,我想要我网站的一部分,比如说 /api/.. 专用于我的 JSON api 以将错误作为 JSON 提供服务,所以如果我输入一个随机的不存在的/api/.. 下的 URL 则返回 404 JSON 响应.

Now, I want a section of my site, lets say /api/.. that is dedicated to my JSON api to serve the errors as JSON, so if I enter a random non-existent URL under /api/.. then it returns 404 JSON response.

是否有任何标准/最佳方法可以做到这一点?我尝试过的一个想法是有一个 @ControllerAdvice 专门捕获我定义并返回 JSON 的一类自定义 API 异常,并在我的标准 ErrorController 中检查 URL 并在以下情况下抛出适当的 API 异常该 API URL 空间(但这不起作用,因为无法调用 ExceptionHandler 方法,因为它与原始控制器方法的返回类型不同).

Is there any standard/best way to do this? One idea I tried out was to have a @ControllerAdvice that specifically caught a class of custom API exceptions I had defined and returned JSON, and in my standard ErrorController checking the URL and throwing an apprpriate API exception if under that API URL space (but that didn't work, as the ExceptionHandler method could not be invoked because it was a different return type from the original controller method).

这件事已经解决了吗?

推荐答案

问题是我自己的错.我试图弄清楚为什么我的 @ExceptionHandler 无法捕获我的异常并返回 JSON - 正如我在问题末尾所建议的那样,我认为我因为返回类型冲突而遇到了问题 -这是不正确的.

The problem was my own fault. I was trying to work out why my @ExceptionHandler was not able to catch my exception and return JSON - As I suggested at the end of my question, I thought I was having problems because of conflicting return types - this was incorrect.

我试图让我的异常处理程序返回 JSON 的错误是这样的:

The error I was getting trying to have my exception handler return JSON was along the lines of:

  "exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
  "message": "Could not find acceptable representation"

我进行了更多的挖掘/实验以尝试缩小问题的范围(认为问题是因为我处于 Spring 错误处理流程和导致问题的 ErrorController 中),然而,问题只是因为 Spring 所做的内容协商.

I did some more digging/experimenting to try to narrow down the problem (thinking that the issue was because I was in the Spring error handling flow and in an ErrorController that was causing the problem), however the problem was just because of the content negotiation stuff Spring does.

因为我在 web.xml 中的 errorPage 映射映射到 /error/404.html,Spring 使用后缀来解析适当的视图 - 所以它然后当我尝试返回 json 时失败.

Because my errorPage mapping in the web.xml was mapping to /error/404.html, Spring was using the suffix to resolve the appropriate view - so it then failed when I tried to return json.

通过将我的 web.xml 更改为 /error/404 或关闭内容协商后缀选项,我已经能够解决该问题.

I have been able to resolve the issue by changing my web.xml to /error/404 or by turning off the content negotiation suffix option.

这篇关于Spring Boot - 处理 JSON 或 HTML 的错误控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆