无法重定向自定义页面中的错误。
配置:
server:
error:
whitelabel:
enabled: false
path: /error
spring:
web:
resources:
add-mappings: false
@RestController
@RequiredArgsConstructor
public class CustomErrorController implements ErrorController {
private static final String PATH = "/error";
@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.NOT_FOUND.value()) {
return "error-404";
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return "error-500";
}
}
return "error";
}
}
你的注释是错误的,应该是
@Controller
,而不是@RestController
- 因为你解析了一个页面,而不是返回一个资源