3

I am trying to execute my Spring Boot application as a runnable jar, it does run fine when running from inside STS but I get 404 when trying to access my JSPs when executing via java -jar.

@SpringBootApplication
public class MyApplication extends WebMvcConfigurerAdapter {

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

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

    @Bean
    //Did trying commenting this part but made no difference
    public ViewResolver jspViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

}

application.properties

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

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.myapp</groupId>
    <artifactId>myapp</artifactId>
    <version>0.1.17-Alpha</version>

    <name>myapp</name>
    <description>My App</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <scope>compile</scope>
        </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-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>retrofit</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I have seen similar questions here but most of them point to the fact that the sufix or prefix is not in the application.properties or tomcat-embed-jasper or even that the jsps are not in the path /src/main/resources/META-INF/resources/WEB-INF/jsp (I do have my JSPs in this path). Any ideas on what I may be missing here ?

Roger that
  • 442
  • 6
  • 21
  • Seems like embeded tomcat does not work well when package is jar as stated here https://stackoverflow.com/questions/21243690/is-it-possible-with-spring-boot-to-serve-up-jsps-with-a-jar-packaging – Roger that Nov 01 '17 at 23:31
  • Tried to use Jetty instead of Tomcat, but no luck so far https://www.mkyong.com/spring-boot/spring-boot-jetty-as-embedded-server/ – Roger that Nov 01 '17 at 23:39
  • try providing something like `com.something.BootApplication` in your `` tag in `pom.xml` – MohamedSanaulla Nov 02 '17 at 05:52
  • Did not work using the tag start-class. – Roger that Nov 03 '17 at 19:09
  • Try this [link](https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html) which says that you have to add `true` in your `spring-boot-maven-plugin` in the `pom` – MohamedSanaulla Nov 05 '17 at 05:37
  • did not help, seems to me that when using JSPs with spring boot there are complications on running it as runnable JAR, seems like it was not thought to be used like that, and instead, the interfaces should be hosted by a separated application, leaving spring boot as an api to be called. For now I will pack it as a war, and publish it in tomcat. As I will be using Docker, this shall not be a big deal as in the end it will be a docker image regardless. – Roger that Nov 05 '17 at 12:32
  • @Roger that : I am also facing the same issue. Did you find any solution to packaged as Jar apart from the used one you mention? – Suresh Apr 02 '19 at 11:55
  • No, for the moment I am deploying it as a war into a Tomcat server. – Roger that Apr 04 '19 at 08:14

1 Answers1

0

I had this problem and for me in gradle I had to change the embedded tomcat dependency from compile to implementation like this

implementation('org.apache.tomcat.embed:tomcat-embed-jasper:8.0.47')
JesseBoyd
  • 1,022
  • 1
  • 13
  • 18