5

I have created a REST API using Spring boot. I am using gradle as my build tool. I have tested the application using the embedded container and it is working fine. I am able to access my application at localhost:8080/foo/bar.

Now I have deployed it to tomcat without any error.

Mar 17, 2016 1:58:47 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /usr/local/apache-tomcat-7.0.65/webapps/foo.war
Mar 17, 2016 1:58:48 PM org.apache.catalina.startup.TldConfig execute
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.
Mar 17, 2016 1:58:48 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /usr/local/apache-tomcat-7.0.65/webapps/foo.war has finished in 623 ms

But I am now unable to access the application at localhost:8080/foo/bar. I can see that tomcat is running at localhost:8080

Here is the build.gradle file.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'war'

war {
    baseName = 'foo'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

configurations{
    providedRuntime
}

dependencies {
   compile 'org.springframework.boot:spring-boot-starter-web'
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
     compile 'com.google.code.gson:gson:2.6.2'
     compile 'org.springframework:spring-webmvc:4.2.5.RELEASE'
     compile 'org.json:json:20151123'

     providedRuntime 'javax.ws.rs:javax.ws.rs-api:2.0.1'
     providedRuntime 'javax.servlet:javax.servlet-api:3.1.0'
     providedRuntime 'javax.servlet:jstl:1.2'

     testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.11'
}

I have an application.properties file with the following code

server.context-path=/foo

and my request mapping in controller is set to

    @CrossOrigin
    @RequestMapping("/bar")
    public String bar(){
       // Code
    }

I tried what is mentioned in this post, but that is also not working.

@SpringBootApplication
public class FooApplication {

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

    // Used when deploying to a standalone servlet container
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(FooApplication.class);
    }
}
Community
  • 1
  • 1
mahacoder
  • 895
  • 3
  • 14
  • 29
  • Why not use embedded tomcat and create executable jar? – SMA Mar 17 '16 at 09:03
  • @SMA I want this application to run on a server. I am not sure if making the application an executable jar will help in that case – mahacoder Mar 17 '16 at 09:06
  • @ak31 It will and imho it will be easier to maintain as the executable jar is ready to be installed as a service. – Arnaud Denoyelle Mar 17 '16 at 09:09
  • 1
    Check [this](https://spring.io/blog/2014/03/07/deploying-spring-boot-applications#what-about-the-java-ee-application-server) for deploying in an application server – seenukarthi Mar 17 '16 at 09:14
  • @ArnaudDenoyelle is that the preferred way of deploying applications? I've always deployed a war file to a tomcat server till now as I wasn't familiar that it could be done like this – mahacoder Mar 17 '16 at 09:15
  • 1
    @ak31 It is a matter of preference. I have several services on my server and I like to be able to start/stop them with `sudo service myProject.jar status/start/stop` like any other services. On the other hand, deployment via a war file is better integrated with continuous integration tools. – Arnaud Denoyelle Mar 17 '16 at 09:19
  • @KarthikeyanVaithilingam your link has worked. I didn't have `extends SpringBootServletInitializer` in the application class. – mahacoder Mar 17 '16 at 09:28
  • @ArnaudDenoyelle having seperate services for applications was something I wasn't aware of. I will definitely explore it further. Thank you. – mahacoder Mar 17 '16 at 09:30

1 Answers1

10

To deploy a Spring Boot application you have to have a configuration class which extends org.springframework.boot.context.web.SpringBootServletInitializer and overrides org.springframework.boot.context.web.SpringBootServletInitializer.configure(SpringApplicationBuilder application)

So when the application is deployed in a JEE Servlet container Spring Boot application will be deployed and served properly.

Example

@Configuration
public class AppServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

The Application.class is same class as in SpringApplication.run(Application.class, args); in the main method

REF: What about the Java EE Application Server?

seenukarthi
  • 8,241
  • 10
  • 47
  • 68