3

I have a spring-boot application and now that I want to deploy the application on a dedicated tomcat server and not using the embedded tomcat. Both ways on deploying I'm not getting any error.

I have given the scope=provided for the spring-boot-starter-tomcat dependency.

When I run the application with embedded tomcat (with appropriate changes made), its working perfectly on hitting the link http://localhost:8080/testGET. But when I ran on the dedicated tomcat with scope=provided, on hitting the link http://localhost:8080/test-results-upload-1.0/testGET or http://localhost:8080/testGET I'm getting the response mapped to /error.

Please help me with this. Not able to understand mistake I'm doing..

Thanks in advance.

 @EnableAutoConfiguration
 @Configuration
 @EnableWebMvc
 @ComponentScan("com............controller")
 @Import(SpringMongoConfig.class)
 public class BootStrap extends SpringBootServletInitializer {

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

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

.

  @Controller
  public class Controller {

     @Autowired(required = true)
     private IRepository config;

     @RequestMapping(value = "/testGET", method = RequestMethod.GET)
     public String testGet(HttpServletResponse response) {
        try {
           response.sendError(HttpStatus.OK.value());
           return "Application working perfectly !";
        } catch (IOException e) {
           e.printStackTrace();
           return null;
        }
     }
  }

.

<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>test-results-upload</groupId>
<artifactId>test-results-upload</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
    <spring.boot>1.1.5.RELEASE</spring.boot>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>${spring.boot}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-webmvc</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring.boot}</version>
        <type>maven-plugin</type>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.2</version>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

Sarath
  • 1,438
  • 4
  • 24
  • 40
  • 1
    Have you checked the catalina out logs to see if there are any errors on startup? And checked logs to see hits are actually coming in? Sometimes when an app is deployed as a WAR, it does not deploy to the root context. So you may need to access it like /test-results-upload/testGET – Bruce Lowe Sep 16 '14 at 14:34
  • Its a clean start and there are no errors. – Sarath Sep 16 '14 at 15:05
  • So, when accessing [http://localhost:8080/test-results-upload-1.0/testGET](http://localhost:8080/test-results-upload-1.0/testGET) I'm getting the response which is mapped to **/error** – Sarath Sep 16 '14 at 15:07
  • Server log `............ 2014-09-16 19:03:55.101 INFO 6240 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/testGET],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com..............Controller.testGet(javax.servlet.http.HttpServletResponse) 2014-09-16 19:03:55.674 INFO 6240 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup ...................... Sep 16, 2014 7:03:57 PM org.apache.catalina.startup.Catalina start ` – Sarath Sep 16 '14 at 15:28
  • What is the exact name of your war ? Or did you look under webapp directory on tomcat where exactly it is deployed ? – Serge Ballesta Sep 16 '14 at 17:19
  • war file name is **test-results-upload-1.0.war** – Sarath Sep 17 '14 at 05:34

4 Answers4

1

BTW, you can add a context path to your stand-alone spring-boot application by setting the following property in you application.properties file:

server.contextPath=/test-results-upload-1.0

This way you will have the same contextPath regardless whether you deployed stand-alone or via an external container.

1

It has been a while since this question was first asked, sharing what I did to get a Spring boot application packaged as war deployed on Tomcat server (External), using Maven:

  1. Deployable war file for Servlet 3.0 API Container (meaning it can be deployed correctly on Tomcat 7.0.x onwards)
    • Environment Setup :
      • Need to have Tomcat 7.0.x version installed, as Support for Servlet 3.0 API is available from Tomcat 7.0.x onwards.
      • Java 1.8 (Make sure to build (JDK8) and run(JRE8 Tomcat JVM target) on the same Java versions).
        • Note:Java 1.7 would also do, just make sure to compile and run on same versions.
    • Code:
      • Boot Application class changes: follow the instructions of extending SpringBootServletInitializer
      • Maven build file pom.xml changes:
        • Change of scope for <artifactId>spring-boot-starter-tomcat</artifactId> to <scope>provided</scope>
        • Finally we need to bundle it as an war file so change of packaging <packaging>war</packaging>
Abhijeet
  • 8,561
  • 5
  • 70
  • 76
0

Remove the call to response.sendError(HttpStatus.OK.value());

Firstly, an HTTP OK (200) response isn't an error and secondly, HTTP OK is the default response status so there's no need to set it. If you want to set it to something other than OK, you should call HttpServletResponse.setStatus.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • I tried that too but still the same problem. `@RequestMapping(value = "/", method = RequestMethod.GET, produces = "text/html") public String index() { return "

    Application working perfectly !

    "; } ` For this also same error.
    – Sarath Sep 16 '14 at 16:20
0

I got the problem resolved by removing (HttpServletResponse response) from the method signature.. Everything else remains the same..

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>

After that get the war file and place in ${TOMCAT_DIR}/webapps and hit the url http://localhost:8080/test-results-upload-1.0/testGET and it worked !!!

Sarath
  • 1,438
  • 4
  • 24
  • 40