I have a Spring Boot app which all of a sudden stops resolving views and instead throws 404 errors at me.
These are my dependencies:
<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>
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);
}
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'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]
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.
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).