gpt4 book ai didi

java - 几分钟后,Spring Boot 停止解析 View

转载 作者:行者123 更新时间:2023-12-03 20:26:44 25 4
gpt4 key购买 nike

我有一个 Spring Boot 应用程序,它突然停止解析 View ,而是向我抛出 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>

该应用程序启动正常,让我可以毫无问题地浏览。但是,如果我停止查看它并继续做事,当我刷新网络浏览器时,我会看到一个 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 (虽然每个返回 View 的 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);
}

然而,这个是可行的,因为它不返回 View ,而是返回原始 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]

Controller 被调用得很好,它完成了它的工作(例如从数据库中获取记录以填充 ModelMap(变量 appVersionprojects,我通过查看日志,在返回之前已将其编辑为“(...)”)。

当应用程序刚刚启动时,我尝试加载的页面工作正常,因此 Controller 本身工作(它一直工作良好,但我一定是引入了导致这一切发生的更改)。在嵌入式 Tomcat 中停止并重新启动应用程序可以解决此问题,并且在将 WAR 部署到独立的 Linux Tomcat 服务器后,该应用程序似乎运行良好(半小时后没有 404)。

最佳答案

我认为关于原始问题的信息不足,但当我面临同样的问题时,这就是我的解决方案:

@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"}); 解决了这个问题。

一旦我将日志记录级别设置为跟踪,我就能够看到问题所在:资源“classpath:/templates/home.html.html was not found”模板中添加了两个后缀。至于为什么它在失败前工作了几分钟,这对我来说仍然是个谜。

希望这对某人有帮助。

关于java - 几分钟后,Spring Boot 停止解析 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36084739/

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