当前位置: 首页 > 知识库问答 >
问题:

Spring Boot(1.3.5)-默认错误视图

印飞捷
2023-03-14

我使用的是Spring Boot(V1.3.5.Release)。在文档中,有一个关于错误处理的简短部分:

Spring Boot默认提供/error映射,以合理的方式处理所有错误,并在servlet容器中注册为“全局”错误页。对于机器客户机,它将生成一个JSON响应,其中包含错误、HTTP状态和异常消息的详细信息。对于浏览器客户端,有一个“whitelabel”错误视图,它以HTML格式呈现相同的数据(要自定义它,只需添加一个解析为“error”的视图)。

因此,要设置我自己的自定义页面,我只需要一个解析为error的视图。我已经将Freemarker添加到类路径中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
@Bean(name = "freeMarkerViewResolver")
public FreeMarkerViewResolver getFreeMarkerViewResolver() {
    FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
    freeMarkerViewResolver.setSuffix(".ftl");
    return freeMarkerViewResolver;
}

@Bean(name = "freemarkerConfigurer")
public FreeMarkerConfigurer getFreeMarkerConfigurer() {
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
    freeMarkerConfigurer.setTemplateLoaderPaths(
            "classpath:/templates/",
            "/WEB-INF/templates/");
    return freeMarkerConfigurer;
}

但是现在,我只获得whitelabel页面,而不是/WEB-INF/templates/error.ftl(例如,当我访问一个无效路径时,如localhost:8080/invalide)。有什么我漏掉的吗?

我很确定BasicErrorController可以处理该作业:

@RequestMapping(produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request,
        HttpServletResponse response) {
    response.setStatus(getStatus(request).value());
    Map<String, Object> model = getErrorAttributes(request,
            isIncludeStackTrace(request, MediaType.TEXT_HTML));
    return new ModelAndView("error", model);
}

这是我的日志:

2016-09-12 08:46:00.241 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Bound request context to thread: org.apache.catalina.connector.RequestFacade@11e1fd30
2016-09-12 08:46:00.241 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/invalid]
2016-09-12 08:46:00.242 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@75739a6e] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.242 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : No handler mapping found for [/invalid]
2016-09-12 08:46:00.242 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@67eec602] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.242 DEBUG 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /invalid
2016-09-12 08:46:00.243 DEBUG 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/invalid]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@605d8da4] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.h.BeanNameUrlHandlerMapping      : No handler mapping found for [/invalid]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@3fa90efa] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.244 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/invalid] are [/**]
2016-09-12 08:46:00.244 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/invalid] are {}
2016-09-12 08:46:00.244 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/invalid] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@16545fd3]]] and 1 interceptor
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@5ef1fb8a]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@781974d0]
2016-09-12 08:46:00.244 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/invalid] is: -1
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : Applying "invalid path" checks to path: invalid
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : Resolving resource for request path "invalid"
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : Checking location: ServletContext resource [/]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : No match for location: ServletContext resource [/]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : Checking location: class path resource [META-INF/resources/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : No match for location: class path resource [META-INF/resources/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : Checking location: class path resource [resources/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : No match for location: class path resource [resources/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : Checking location: class path resource [static/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : No match for location: class path resource [static/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : Checking location: class path resource [public/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver    : No match for location: class path resource [public/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : No matching resource found - returning 404
2016-09-12 08:46:00.245 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@11e1fd30
2016-09-12 08:46:00.246 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Bound request context to thread: org.apache.catalina.core.ApplicationHttpRequest@292356a6
2016-09-12 08:46:00.246 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@75739a6e] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : No handler mapping found for [/error]
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@67eec602] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.246 DEBUG 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2016-09-12 08:46:00.247 TRACE 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Found 2 matching mapping(s) for [/error] : [{[/error],produces=[text/html]}, {[/error]}]
2016-09-12 08:46:00.248 DEBUG 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2016-09-12 08:46:00.248 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@5ef1fb8a]
2016-09-12 08:46:00.248 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/error] is: -1
2016-09-12 08:46:00.248 TRACE 5888 --- [nio-8080-exec-4] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking [BasicErrorController.errorHtml] method with arguments [org.apache.catalina.core.ApplicationHttpRequest@292356a6, org.apache.catalina.connector.ResponseFacade@4fa94295]
2016-09-12 08:46:00.248 TRACE 5888 --- [nio-8080-exec-4] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [errorHtml] returned [ModelAndView: reference to view with name 'error'; model is {timestamp=Mon Sep 12 08:46:00 SGT 2016, status=404, error=Not Found, message=No message available, path=/invalid}]
2016-09-12 08:46:00.249 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2016-09-12 08:46:00.249 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@58edc30] based on requested media type 'text/html'
2016-09-12 08:46:00.249 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@58edc30] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.249 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Cleared thread-bound request context: org.apache.catalina.core.ApplicationHttpRequest@292356a6
2016-09-12 08:46:00.249 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Successfully completed request

共有1个答案

巫马磊
2023-03-14

您可以使用Spring-Boot的属性spring.freemarker.template-loader-path,而不是重新定义freemarker beans(这可能是错误的原因),如下所述:

如果您使用FreeMarker,您还将有一个id为“Free MarkerViewResolver”的FreeMarkerViewResolver。它通过用前缀和后缀包围视图名称(分别用empty和'.ftl'默认值外化为spring.freemarker.prefix和spring.freemarker.suffix),在加载器路径(外化为spring.freemarker.templateLoaderpath,默认为'classpath://templates/')中查找资源。可以通过提供同名bean来重写它。

默认值为:

spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
spring.freemarker.template-loader-path=classpath:/WEB-INF/templates/
 类似资料:
  • 有一个示例应用程序并创建了一个 页面。从我的控制器,我返回以下内容 在调试模式下,我收到这个警告/错误: 我的src/main/webapp/WEB-INF/spring/appServlet/servlet-context的内容.xml 如果我将.html重命名为 .jsp 并将上面更改为 .jsp,那么一切正常。

  • 操作步骤: ①登录账号进入工作台,选择地图点击编辑进入编辑地图页面,点击锁定默认视图。 提示 ●锁定视图后,退出地图再次进入显示默认锁定的视图。 操作动图: [查看原图]

  • 问题内容: 我的桌子看起来像 然后我有一个触发器来自动填充CREATED_BY字段 当我使用插入 该条目在表中进行,但我仍然收到错误消息 有没有一种方法可以抑制此错误,而又不使字段可为空并且不删除触发器?否则,我的hibernate状态将看到这些异常(即使已进行插入),然后应用程序将崩溃。 问题答案: 设置默认值(例如:empty ),触发器将始终更新该值。

  • 我的桌子看起来像 然后我有一个触发器来自动填充CREATED_BY字段 当我使用 表中有条目,但我仍然得到错误消息 有没有一种方法可以在不使字段为空和不移除触发器的情况下抑制此错误?否则,我的hibernate将看到这些异常(即使已经进行了插入),然后应用程序将崩溃。

  • 但仍然不起作用。下面是我的Android Manifest.xml文件。我想用名为“.main2activity”的活动启动我的应用程序。还要注意,出于某种原因,我的.xml文件在设计模式下显示为黑色背景,而且奇怪地不同。

  • 接口说明 设置场景默认视角 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 POST /api/viewpoint/1.0.0/setSceneDefault 是否需要登录 是 请求字段说明 参数 类型 请求类型 是否必须 说明 dId string form 是 场景ID directionx number form 是 相机位置,从cam