1

I have spring annotated controllers that work fine when I am using my WAR, but when I try to run embedded, locally and on Heroku, none of the annotated controllers are working. I have some pages setup using mvc:view-controller and those work, but none of the component-scan controllers work.

package com.myapp.launch;

import java.io.File;

import javax.servlet.ServletException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String webappDirLocation = "src/main/webapp/";
        Tomcat tomcat = new Tomcat();

        //The port that we should run on can be set into an environment variable
        //Look for that variable and default to 8080 if it isn't there.
        String webPort = System.getenv("PORT");
        if(webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }

        tomcat.setPort(Integer.valueOf(webPort));

        try {
            tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        tomcat.getServer().await();  


    }

}

Here is part of my spring config.

<mvc:view-controller path="/" view-name="home"/>
<mvc:view-controller path="/terms" view-name="terms"/>
<mvc:view-controller path="/privacy" view-name="privacy"/>

<context:component-scan base-package="com.myapp.controllers"/>
zmanc
  • 5,201
  • 12
  • 45
  • 90

1 Answers1

0

I found out that this was due to my controllers being groovy and those controllers were being compiled as part of a make step when I was running tomcat locally, but that same process was not being run when I launched tomcat embedded. After adding an execution goal to my gmaven plugin I was able to get this working without issue.

Since the classes were compiled by gmaven tomcat was able to pick them up.

zmanc
  • 5,201
  • 12
  • 45
  • 90