0

I have a springboot app that serves a webpage just fine when running locally but for some reason returns 404 when I package a deploy the war file to production env, I get a 404 error. In my controller, I return a jsp page that includes an html page from my /resources/static folder. This all works fine locally.

I'm deploying to tomcat 7.0.52

this is my pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <spring.version>4.3.9.RELEASE</spring.version>
    <java.version>1.8</java.version>
    <maven-compiler.version>3.6.1</maven-compiler.version>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <!-- //////////////////////////////////////////////////////// -->
    <!-- SPRING -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
        <version>7.0.19</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>
                            repackage
                        </goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

`

My Springboot app's main class

@SpringBootApplication
public class SpringbootApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SpringbootApplication.class);
}

 public static void main(String[] args) throws Exception {
    SpringApplication.run(SpringbootApplication.class, args);
 }
}

My class that extends webmvcConfigureAdapter

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    System.out.println("in mvc config");
    registry.addViewController("/").setViewName("forward:index2.html");
}

@Override
public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
    System.out.println("configure serve handling");
    configurer.enable();
}  

@Bean
InternalResourceViewResolver internalResourceViewResolver () {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}
}

My main controller.

@Controller
public class MainController {
    @RequestMapping(value={"/"}, method = RequestMethod.GET)
    public String index(){
        System.out.println("inside mainController");
        return "chatbot";
    }
}

Finally my application.properties file looks like this.

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

The structure of my project looks like this...

src/main/webapp
               -> assets
               -> resources
                   -> static ->index.html
               -> WEB-INF      
                   ->jsp -> index.jsp

Please help. I've been stuck on this for over 8 hrs now. Thanks.

jackjoesmith
  • 951
  • 4
  • 20
  • 33

2 Answers2

0

YOu can place your index.html under webapp since this is the default contextpath, all other resources should be relevant to it.

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
-1

Maybe the problem is that you have to include the application context path.

Check the following link to do that add-context-path-to-spring-boot-application

  • Your answer should be able to stand on its own if, for example, that link goes dead. If you provide the basic outline and a link for more information, that is adequate, but link-only answers are frowned on. – dcsohl Aug 09 '17 at 16:01