Spring boot - taking control of 404 Not Found

2019-06-22 06:32发布

I'm trying to figure out the simplest way to take control over the 404 Not Found handler of a basic Spring Boot RESTful service such as the example provided by Spring:

https://spring.io/guides/gs/rest-service/

Rather than have it return the default Json output:

{
  "timestamp":1432047177086,
  "status":404,
  "error":"Not Found",
  "exception":"org.springframework.web.servlet.NoHandlerFoundException",
  "message":"No handler found for GET /aaa, ..."
}

I'd like to provide my own Json output.

By taking control of the DispatcherServlet and using DispatcherServlet#setThrowExceptionIfNoHandlerFound(true), I was able to make it throw an exception in case of a 404 but I can't handle that exception through a @ExceptionHandler, like I would for a MissingServletRequestParameterException. Any idea why?

Or is there a better approach than having a NoHandlerFoundException thrown and handled?

4条回答
神经病院院长
2楼-- · 2019-06-22 06:35

The @EnableWebMvc based solution can work, but it might break Spring boot auto configurations. The solution I am using is to implement ErrorController:

@RestController
@RequiredArgsConstructor
public class MyErrorController implements ErrorController {

        private static final String ERROR_PATH = "/error";

        @NonNull
        private final ErrorAttributes errorAttributes;

        @RequestMapping(value = ERROR_PATH)
        Map<String, Object> handleError(WebRequest request) {
            return errorAttributes.getErrorAttributes(request, false);
        }

        @Override
        public String getErrorPath() {
            return ERROR_PATH;
        }
}
查看更多
来,给爷笑一个
3楼-- · 2019-06-22 06:41

According to the Spring documentation appendix A. there is a boolean property called spring.mvc.throw-exception-if-no-handler-found which can be used to enable throwing NoHandlerFoundException. Then you can create exception handler like any other.

@RestControllerAdvice
class MyExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(MyExceptionHandler.class);

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public String handleNoHandlerFoundException(NoHandlerFoundException ex) {
        log.error("404 situation detected.",ex);
        return "Specified path not found on this server";
    }
}

@ExceptionHandler itself without @ControllerAdvice (or @RestControllerAdvice) can't be used, because it's bound to its controller only.

查看更多
够拽才男人
4楼-- · 2019-06-22 06:42

It works perfectly Fine.

When you are using SpringBoot it does not handle (404 Not Found) explicitly it uses WebMvc error response. If your Spring Boot should handle that exception then you should do some hack around Spring Boot. For 404 Exception class is NoHandlerFoundException if you want to handle that exception in your @RestControllerAdvice Class you must add @EnableWebMvc annotation in your Application class and set setThrowExceptionIfNoHandlerFound(true); in DispatcherServlet. Please Refer the code

@SpringBootApplication
@EnableWebMvc
public class Application {  
    @Autowired
    private DispatcherServlet servlet;

    public static void main(String[] args) throws FileNotFoundException, IOException {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner getCommandLineRunner(ApplicationContext context) {
        servlet.setThrowExceptionIfNoHandlerFound(true);
        return args -> {};
    }
}

After this you can handle NoHandlerException in your @RestControllerAdvice class

@RestControllerAdvice
public class AppException {

    @ExceptionHandler(value={NoHandlerFoundException.class})
    @ResponseStatus(code=HttpStatus.BAD_REQUEST)
    public ApiError badRequest(Exception e, HttpServletRequest request, HttpServletResponse response) {
        e.printStackTrace();
        return new ApiError(400, HttpStatus.BAD_REQUEST.getReasonPhrase());
    }
}   

I have created ApiError class to return customized error response

public class ApiError {
    private int code;
    private String message;
    public ApiError(int code, String message) {
        this.code = code;
        this.message = message;
    }
    public ApiError() {
    }   
    //getter & setter methods...
}
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-06-22 06:50

In short, the NoHandlerFoundException is thrown from the Container, not from your application within your container. Therefore your Container has no way of knowing about your @ExceptionHandler as that is a Spring feature, not anything from the container.

What you want is a HandlerExceptionResolver. I had the very same issue as you, have a look at my solution over there: How to intercept "global" 404s on embedded Tomcats in spring-boot

查看更多
登录 后发表回答