我有一个Spring Boot应用程序,突然停止解析视图,而是向我抛出404错误。
这些是我的依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- Jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Hibernate validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- BoneCP -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>${bonecp.version}</version>
</dependency>
<!-- MySql Java Connector -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/ojdbc14-10.2.0.4.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
该应用程序启动正常,让我浏览没有任何问题。但是,如果我停止查看并继续执行操作,当我刷新Web浏览器时,我会看到Tomcat 404页面:
HTTP Status 404 - /WEB-INF/jsp/home.jsp
type: Status report
message: /WEB-INF/jsp/home.jsp
description: The requested resource is not available.
Apache Tomcat/7.0.52
Home Controller(虽然每个返回视图的控制器都失败了):
@Controller
public class ProjectOutputController {
private static final Logger LOGGER = LoggerFactory.getLogger(ProjectOutputController.class);
private final ProjectOutputService projectOutputService;
@Value("${app.version}")
private String appVersion;
@Inject
public ProjectOutputController(final ProjectOutputService projectOutputService) {
this.projectOutputService = projectOutputService;
}
@RequestMapping(value={"/", "/home", "/results"})
public ModelAndView getHomeView(){
LOGGER.trace("Returning main view");
ModelMap map = new ModelMap();
List<ProjectOutput> projectOutputs = projectOutputService.getProjects();
map.addAttribute("projects", projectOutputs);
map.addAttribute("appVersion", appVersion);
return new ModelAndView("home",map);
}
然而,这个可以工作,因为它不会返回视图,而是返回原始JSON作为字符串
@RequestMapping(method=RequestMethod.GET, value={"/overview/{projectId}"}, produces = "application/json")
@ResponseBody
public ResponseEntity<ProjectOverview> showProjectJson(@PathVariable String projectId) {
LOGGER.trace("Returning JSON for project ID " + projectId + " view");
try {
return new ResponseEntity<> (projectOverviewService.getProjectbyId(projectId), HttpStatus.OK);
} catch (ProjectOverviewNotFoundException e) {
LOGGER.error("Project not found - " + e);
return new ResponseEntity<> (HttpStatus.NOT_FOUND);
}
}
}
Spring Boot的跟踪日志:
2016-03-18 12:49:26 TRACE o.s.w.s.v.InternalResourceViewResolver:164 - Cached view [home]
2016-03-18 12:49:26 DEBUG o.s.w.s.v.ContentNegotiatingViewResolver:402 - Returning [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB-INF/jsp/home.jsp]] based on requested media type 'text/html'
2016-03-18 12:49:26 DEBUG o.s.w.s.DispatcherServlet:1214 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB-INF/jsp/home.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2016-03-18 12:49:26 TRACE o.s.w.s.v.JstlView:261 - Rendering view with name 'home' with model {projects=[ (...) ], appVersion=1.1} and static attributes {}
2016-03-18 12:49:26 DEBUG o.s.w.s.v.JstlView:377 - Added model object 'projects' of type [java.util.ArrayList] to request in view with name 'home'
2016-03-18 12:49:26 DEBUG o.s.w.s.v.JstlView:377 - Added model object 'appVersion' of type [java.lang.String] to request in view with name 'home'
2016-03-18 12:49:26 DEBUG o.s.w.s.v.JstlView:207 - Forwarding to resource [/WEB-INF/jsp/home.jsp] in InternalResourceView 'home'
2016-03-18 12:49:26 TRACE o.s.t.s.TransactionSynchronizationManager:243 - Removed value [org.springframework.orm.jpa.EntityManagerHolder@1b217c9] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@17b412f] from thread [http-nio-8080-exec-1]
2016-03-18 12:49:26 DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor:112 - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2016-03-18 12:49:26 DEBUG o.s.o.j.EntityManagerFactoryUtils:435 - Closing JPA EntityManager
2016-03-18 12:49:26 TRACE o.h.i.SessionImpl:353 - Closing session
2016-03-18 12:49:26 TRACE o.h.e.j.i.JdbcCoordinatorImpl:190 - Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@d45342]
2016-03-18 12:49:26 TRACE o.h.e.j.i.LogicalConnectionImpl:178 - Closing logical connection
2016-03-18 12:49:26 DEBUG o.h.e.j.i.LogicalConnectionImpl:246 - Releasing JDBC connection
2016-03-18 12:49:26 DEBUG o.h.e.j.i.LogicalConnectionImpl:264 - Released JDBC connection
2016-03-18 12:49:26 TRACE o.h.e.j.i.LogicalConnectionImpl:190 - Logical connection closed
2016-03-18 12:49:26 TRACE o.s.w.s.DispatcherServlet:1053 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1b8ac37
2016-03-18 12:49:26 DEBUG o.s.w.s.DispatcherServlet:991 - Successfully completed request
2016-03-18 12:49:26 TRACE o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext:331 - Publishing event in org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@121bfd2: ServletRequestHandledEvent: url=[/]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcherServlet]; session=[null]; user=[null]; time=[390ms]; status=[OK]
控制器被称为正常,它完成它的工作(例如从数据库中提取记录以填充ModelMap(变量appVersion
和projects
,我已将其编辑到&#34 ;(...)&#34;)在返回之前)通过查看日志。
我尝试加载的页面在应用程序刚刚启动时工作正常,因此控制器本身可以工作(它一直运行良好,但我必须引入一个导致所有这一切发生的变化) 。在嵌入式Tomcat中停止并重新启动应用程序可以解决问题,并且在将WAR部署到独立的Linux Tomcat服务器时,应用程序似乎运行良好(半小时内没有404)。
答案 0 :(得分:0)
我认为原始问题上没有足够的信息,但是当我面对同样的问题时,这是我的解决方案:
@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCacheable(true);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new LayoutDialect());
templateEngine.addDialect(new SpringSecurityDialect());
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(0);
// viewResolver.setViewNames(new String[] { "*.html", "*.xhtml" });
return viewResolver;
}
删除viewResolver.setViewNames(new String[] { "*.html", "*.xhtml" });
解决了问题。
一旦我将日志记录级别设置为跟踪,我就能看到问题:
Resource "classpath:/templates/home.html.html was not found"
模板中添加了两个后缀。至于为什么它在失败之前工作了几分钟对我来说仍然是一个谜。
希望这有助于某人。