gpt4 book ai didi

java - Spring : Default html error page is not properly displayed?

转载 作者:行者123 更新时间:2023-12-02 02:33:59 24 4
gpt4 key购买 nike

当出现 404 、 406 等错误时,我想将我的网站重定向到默认错误页面。我按照示例并在 Controller 中编写了以下方法。

@Controller
public class HomeController {
private static final String PATH = "/error-page";

public String getErrorPath() {
return PATH;
}

@RequestMapping(method=RequestMethod.GET, value=PATH, produces="text/plain")
@ResponseBody
public Map<String, Object> handleGet(HttpServletRequest request) {

Map<String, Object> map = new HashMap<String, Object>();
map.put("status", request.getAttribute("javax.servlet.error.status_code"));
map.put("reason", request.getAttribute("javax.servlet.error.message"));

return map;
}

@RequestMapping(method=RequestMethod.POST, value=PATH, produces="text/plain")
@ResponseBody
public Map<String, Object> handlePost(HttpServletRequest request) {

Map<String, Object> map = new HashMap<String, Object>();
map.put("status", request.getAttribute("javax.servlet.error.status_code"));
map.put("reason", request.getAttribute("javax.servlet.error.message"));

return map;
}
}

在上述路径中有一个html页面,名为error-page.html。虽然我没有收到错误,但在收到错误时无法显示此错误页面。相反,会显示通常的错误消息。这里有什么问题呢?

非常感谢任何帮助。

最佳答案

我不知道“...显示通常的错误消息”是否指的是白标签错误页面。但如果您想停用此功能并显示您自己的错误页面,您必须放入 application.properties 或 yml:

server.error.whitelabel.enabled=false

然后添加一个ErrorController,如下所示:

@Controller
public class MyErrorController implements ErrorController {

private static final String PATH = "/error";

public MyErrorController() {}

@RequestMapping(value = PATH)
public ModelAndView handleAllException() {
ModelAndView modelAndView = new ModelAndView("error_template");
return modelAndView;
}

@Override
public String getErrorPath () {
System.out.println("-- Error Page GET --");
return "error";
}

}

关于java - Spring : Default html error page is not properly displayed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46661613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com