Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JavaConfig and Tomcat 8

I have a Spring 4 web application (webapp-module.war) working and running locally in eclipse using Java 8, tomcat 8 and JavaConfig (no web.xml):

enter image description here

But when I deploy to tomcat 8 (same version I am using locally in eclipse) on a remote Ubuntu server I get:

enter image description here

I verified host and port which are correct. There is no error in the log (/var/lib/tomcat8/logs/catalina.out)

Jun 21, 2016 10:32:44 PM org.apache.catalina.startup.HostConfig undeploy
INFO: Undeploying context [/webapp-module]
Jun 21, 2016 10:32:44 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat8/webapps/webapp-module.war
Jun 21, 2016 10:32:46 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jun 21, 2016 10:32:46 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /var/lib/tomcat8/webapps/webapp-module.war has finished in 1,870 ms
root@vmi63860:/var/lib/tomcat8/logs# 

The access log contains:

root@vmi63860:/var/log/tomcat8# cat localhost_access_log.2016-06-22.txt 
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:00 +0200] "GET /webapp-module/ HTTP/1.1" 404 1040
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:00 +0200] "GET /favicon.ico HTTP/1.1" 404 1034
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:50 +0200] "GET /webapp-module/hello HTTP/1.1" 404 1050

Where xx.xxx.xxx.xx is the IP of my local machine from where I try to access the web app in my browser.

I took a look at: Spring Java Config: Tomcat deploy without web.xml but it does not really provide a solution.

Details on my project below:

Sources

enter image description here

Config.java

@Configuration // Marks this class as configuration
// Specifies which package to scan
@ComponentScan("com.samples")
// Enables Spring's annotations
@EnableWebMvc
public class Config {

  @Bean
  public UrlBasedViewResolver setupViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
  }

}

WebInitializer.java

public class WebInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(Config.class);
    ctx.setServletContext(servletContext);

    Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);

  }

}

HelloController.java

@Controller
public class HelloController {

  @RequestMapping("/")
  public String home() {
    return "index";
  }

  @RequestMapping("/hello")
  public String showhello(ModelMap model) {
    model.addAttribute("message", "Hello Spring MVC Framework!");
    return "hello";
  }

}
like image 481
u123 Avatar asked Jun 21 '16 20:06

u123


People also ask

Is spring boot compatible with Tomcat 10?

* names). Spring Boot 2 and Spring 5 support only the previous Java EE 8 specification, you need to wait for Spring Boot 3 and Spring 6 for Tomcat 10 support. Alternatively you can pass Spring libraries through the Apache Tomcat Migration Tool, which just reached version 1.0 or downgrade to Tomcat 9.0.

Does spring 5 supported Tomcat version?

As @procrastinate_later points out, Spring 5 actually requires Servlet 3.1 (and Tomcat 8.5. x). So you need to upgrade to at least Tomcat 7. Servlet 3.1 is required.

Does Tomcat support spring Framework?

The combination of world class support for Spring and Tomcat provides a valuable service to IT organizations seeking to leverage the Spring Framework on the Tomcat platform.

Can Tomcat run spring boot application?

React Full Stack Web Development With Spring BootBy using Spring Boot application, we can create a war file to deploy into the web server. In this chapter, you are going to learn how to create a WAR file and deploy the Spring Boot application in Tomcat web server.


1 Answers

Sorry previously I was too hasty

It seems that spring context is not at all loaded.

I guess the problem is in this piece of code:

public class WebInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(Config.class);
    ctx.setServletContext(servletContext);

    Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);

  }

}

You used ctx.register(Config.class);

In any case I always used this kind of initialization:

public class AppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.scan("com.spring");
        rootContext.setConfigLocations(new String[]{"com.spring.config.WebAppContextConfig", "com.spring.config.AppConfig"});
        // Manages the lifecycle of the root application context
        servletContext.addListener(new ContextLoaderListener(rootContext));

        // Declare dispatcher servlet. Handles requests into the application
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
                new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }

}

As you can see I used rootContext.setConfigLocations in order to specify where to find the spring configuration classes

In any case Here you can find a working sample I successfully deployed it on tomcat 8.0.39 and 8.5.4

Hope it's useful

Angelo

like image 158
Angelo Immediata Avatar answered Sep 28 '22 18:09

Angelo Immediata