几分钟后Spring Boot停止解析视图 [英] Spring Boot stops resolving views after a few minutes

查看:94
本文介绍了几分钟后Spring Boot停止解析视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot应用程序,该应用程序突然停止解析视图,而是向我抛出404错误.

I have a Spring Boot app which all of a sudden stops resolving views and instead throws 404 errors at me.

这些是我的依赖项:

    <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页面:

The app starts fine, and lets me browse without any issue. However if I stop looking at it and just continue doing stuff, when I refresh the web browser I see a Tomcat 404 page:

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 (Although every controller that returns a view fails):

@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作为字符串

This one works, however, as it does not return a view but a raw JSON as string

@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的跟踪日志:

Spring Boot's trace log:

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(变量appVersionprojects,我已将其编辑为(...)" ),然后返回日志).

The controller gets called fine, and it does its job (Such as fetching records from the DB in order to populate the ModelMap (variables appVersion and projects, which I have redacted to "(...)" ) before returning it) by looking at the log.

刚启动应用程序时,我要加载的页面工作正常,因此控制器本身也可以工作(它一直运行良好,但我必须进行了更改,导致所有这些事情发生).停止并重新启动嵌入式Tomcat中的应用程序可以解决此问题,并且在将WAR部署到独立的Linux Tomcat服务器上后,该应用程序似乎运行良好(半个小时,没有404秒).

The page I'm trying to load works fine when the app has just been started, so the controller itself works (It has been working well always, but I must have introduced a change that caused all this to happen). Stopping and restarting the app in the embedded Tomcat fixes the issue, and upon deploying a WAR to a standalone Linux Tomcat server, the app seems to work well (Half an hour in and no 404s).

推荐答案

我认为原始问题的信息不足,但是由于我正面临着同样的问题,这是我的解决方案:

I don't think there is enough info on the original question but as I was facing this exact same issue this was the solution for me:

@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" 模板中添加了两个后缀.至于为什么它在失败前能工作几分钟,对我来说仍然是个谜.

Once I set the logging level to trace I was able to see the problem: Resource "classpath:/templates/home.html.html was not found" There were two suffixes being added to the templates. As to why it worked for a few minutes before failing is still a mystery to me.

希望这对某人有帮助.

这篇关于几分钟后Spring Boot停止解析视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆