[英]How to document @ControllerAdvice handled exception using Spring REST Docs
[英]404 Exception not handled in Spring ControllerAdvice
我有一個簡單的 Spring MVC 應用程序,我想在其中使用@ControllerAdvice
處理所有未映射的 url。 這是 controller:
@ControllerAdvice
public class ExceptionHandlerController {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public String handle404() {
return "exceptions/404page";
}
}
盡管如此,每次都會得到 Whitelabel Error Page。
我嘗試使用RuntimeException.class
、 HttpStatus.BAD_REQUEST
並使用 NoHandlerFoundException 擴展NoHandlerFoundException
但沒有用。
有什么建議么?
為了使它工作,你需要設置throwExceptionIfNoHandlerFound
上DispecherServlet財產。 你可以這樣做:
spring.mvc.throwExceptionIfNoHandlerFound=true
在application.properties
文件中,否則請求將始終轉發到默認的servlet,並且將拋出NoHandlerFoundException。
問題是,即使使用此配置,它也不起作用。 從文檔:
請注意,如果使用org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,則請求將始終轉發到默認servlet,並且在這種情況下永遠不會拋出NoHandlerFoundException。
因為Spring Boot默認使用org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
你必須使用自己的WebMvcConfigurer
覆蓋它:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
// Do nothing instead of configurer.enable();
}
}
當然,在您的情況下,上述類可能會更復雜。
另一種方法是ErrorController
@Controller
public class MyErrorController implements ErrorController {
@GetMapping("/error")
public ModelAndView errorHandler(HttpServletRequest req) {
// Get status code to determine which view should be returned
Object statusCode = req.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
// In this case, status code will be shown in a view
ModelAndView mav = new ModelAndView("error_default");
mav.addObject("code", statusCode.toString());
return mav;
}
public String getErrorPath() {
return "/error";
}
}
在application.properties中添加以下行
spring.mvc.throwExceptionIfNoHandlerFound=真
和@EnableWebMvc與@ControllerAdvice ,注意:在 handleNoHandlerFoundException 方法上添加 @Override 這對我有用!
@EnableWebMvc
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {
CustomResponse response = new CustomResponse();
response.setTimestamp(LocalDateTime.now());
response.setMessage(ApplicationErrorCodes.NO_HANDLER_FOUND.getErrorMessage());
response.setStatus(HttpStatus.BAD_REQUEST);
response.setErrorCode(ApplicationErrorCodes.NO_HANDLER_FOUND.getErrorCode());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.