我正在使用Spring Boot(v1.3.5.RELEASE)。在文档中,有关于错误处理的this short section:
Spring Boot默认提供 / error 映射,以合理的方式处理所有错误,并在servlet容器中注册为“全局”错误页面。对于机器客户端,它将生成一个JSON响应,其中包含错误,HTTP状态和异常消息的详细信息。对于浏览器客户端,有一个“whitelabel”错误视图,以HTML格式呈现相同的数据(要自定义它,只需添加一个解析为'错误'的视图)。
因此,要设置我自己的自定义页面,我只需要一个解析为error
的视图。我已将Freemarker添加到我的类路径中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
果然,在error.ftl
中创建一个名为src/main/resources/templates
的文件可以帮我找到自己的自定义页面。
但是,我不想在error.ftl
文件夹中添加resources/templates
,而是将其放在WEB-INF/templates
内。
所以,我移动了文件(现在在WEB-INF/templates/error.ftl
中)并创建了以下与spring配置相关的bean:
@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/invalid
)。我错过了什么吗?
我很确定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
答案 0 :(得分:1)
您可以使用spring-boot的属性spring.freemarker.template-loader-path
,而不是重新定义freemarker bean(可能是您的错误的原因),如here所述:
如果您使用FreeMarker,您还可以使用FreeMarkerViewResolver id'freeMarkerViewResolver'。它在加载器路径中查找资源 (外部化为 spring.freemarker.templateLoaderPath ,默认 'classpath:/ templates /')通过用前缀包围视图名称 和后缀(外化到spring.freemarker.prefix和 spring.freemarker.suffix,空和'.ftl'默认值 分别)。它可以通过提供相同的bean来覆盖 名。
默认为:
spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
您可以将其添加到您的application.properties:
spring.freemarker.template-loader-path=classpath:/WEB-INF/templates/