简体   繁体   English

Spring Boot返回Whitelabel错误页面而不是JSON

[英]Spring Boot returns Whitelabel Error Page instead of JSON

I need RESTful service to return JSON every time regardless if there is error or not. 我需要RESTful服务每次都返回JSON,无论是否存在错误。 With normal cases everything goes ok, but when exceptions come I see Whitelabel Error Page . 在正常情况下,一切正常,但是当出现异常时,我会看到Whitelabel Error Page

I tried to resolve this in two ways. 我试图以两种方式解决这个问题。

Through @ExceptionHandler inside @Controller class inside the class annotated with @RestController: 通过@Controller类内部的@ExceptionHandler并在@RestController注释的类中:

@ExceptionHandler(MyCustomException.class)
@ResponseBody
public ErrorResponse handleException(Exception e) {
    return new ErrorResponse(5, "Error message");

}

And through special class with @ControllerAdvice annotation: 并通过带有@ControllerAdvice批注的特殊类:

@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(MyCustomException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ErrorResponse handleSecurityException(MyCustomException e) {
        return new ErrorResponse(5, "Error message");
    }

}

In both cases through breakpoints I see that those methods was called, but I still receive Whitelabel Error Page . 在这两种情况下,我都通过断点看到了这些方法,但是我仍然收到Whitelabel Error Page

So what am I missing or doing wrong? 那我想念什么或做错什么呢?

问题是我忘了为ErrorResponse类实现getter和setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM