GA1
GA1

Reputation: 1627

No errors, but mapping does not work Spring boot + tomcat 8.0

I wanted to create a tiny app running on spring boot. When I package it into a jar file the app runs smoothly on my local machine. I decided to deploy it on AWS elastic beanstalk, so I did the changes needed for creation of .war file (https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html)

I wanted to try it running on my local TOMCAT. So i placed the war file in the webapps directory, and run the Tomcat 8.0\bin\Tomcat8.exe

The logs in the console look ok, there is even an info that the /hello route got mapped. The server runs, but when I enter the localhost:8080/hello nothing is returned. No error, just an empty page.

...........

rtInternal Starting Servlet Engine: Apache Tomcat/8.0.32
13-Feb-2016 23:57:32.965 INFO [localhost-startStop-1] org.apache.catalina.startu
p.HostConfig.deployWAR Deploying web application archive C:\Program Files\Apache
 Software Foundation\Tomcat 8.0\webapps\superheroes-0.0.1-SNAPSHOT.war
13-Feb-2016 23:57:43.058 INFO [localhost-startStop-1] org.apache.jasper.servlet.
TldScanner.scanJars 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 scan
ned but no TLDs were found in them. Skipping unneeded JARs during scanning can i
mprove startup time and JSP compilation time.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.2.RELEASE)

2016-02-13 23:57:46.723  INFO 7804 --- [ost-startStop-1] com.ga1.SuperHeroesAppl
ication           : Starting SuperHeroesApplication v0.0.1-SNAPSHOT on Kamil-PC
with PID 7804 (started by Kamil in C:\Program Files\Apache Software Foundation\T
omcat 8.0)
2016-02-13 23:57:46.734  INFO 7804 --- [ost-startStop-1] com.ga1.SuperHeroesAppl
ication           : No active profile set, falling back to default profiles: def
ault
2016-02-13 23:57:47.076  INFO 7804 --- [ost-startStop-1] ationConfigEmbeddedWebA
pplicationContext : Refreshing org.springframework.boot.context.embedded.Annotat
ionConfigEmbeddedWebApplicationContext@1935808: startup date [Sat Feb 13 23:57:4
7 ICT 2016]; root of context hierarchy
2016-02-13 23:57:49.030  INFO 7804 --- [ost-startStop-1] o.s.b.f.s.DefaultListab
leBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' w
ith a different definition: replacing [Root bean: class [null]; scope=; abstract
=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=tru
e; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.Err
orMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanN
ameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in c
lass path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfi
guration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null];
 scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; auto
wireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.auto
configure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMet
hodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred);
 defined in class path resource [org/springframework/boot/autoconfigure/web/WebM
vcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-02-13 23:57:49.775  INFO 7804 --- [ost-startStop-1] o.s.web.context.Context
Loader            : Root WebApplicationContext: initialization completed in 2701
 ms
2016-02-13 23:57:50.957  INFO 7804 --- [ost-startStop-1] b.a.w.TomcatWebSocketCo
ntainerCustomizer : NonEmbeddedServletContainerFactory detected. Websockets supp
ort should be native so this normally is not a problem.
2016-02-13 23:57:51.674  INFO 7804 --- [ost-startStop-1] o.s.b.c.e.ServletRegist
rationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-02-13 23:57:51.676  INFO 7804 --- [ost-startStop-1] o.s.b.c.embedded.Filter
RegistrationBean  : Mapping filter: 'errorPageFilter' to: [/*]
2016-02-13 23:57:51.677  INFO 7804 --- [ost-startStop-1] o.s.b.c.embedded.Filter
RegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-02-13 23:57:51.678  INFO 7804 --- [ost-startStop-1] o.s.b.c.embedded.Filter
RegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-02-13 23:57:51.680  INFO 7804 --- [ost-startStop-1] o.s.b.c.embedded.Filter
RegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-02-13 23:57:51.681  INFO 7804 --- [ost-startStop-1] o.s.b.c.embedded.Filter
RegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-02-13 23:57:52.361  INFO 7804 --- [ost-startStop-1] s.w.s.m.m.a.RequestMapp
ingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.cont
ext.embedded.AnnotationConfigEmbeddedWebApplicationContext@1935808: startup date
 [Sat Feb 13 23:57:47 ICT 2016]; root of context hierarchy
2016-02-13 23:57:52.635  INFO 7804 --- [ost-startStop-1] s.w.s.m.m.a.RequestMapp
ingHandlerMapping : Mapped "{[/superHero],methods=[POST]}" onto public org.sprin
gframework.http.ResponseEntity<java.lang.Void> com.ga1.superheroes.MainControlle
r.createSuperHero(com.ga1.superheroes.repository.RepositorySuperHero)
2016-02-13 23:57:52.639  INFO 7804 --- [ost-startStop-1] s.w.s.m.m.a.RequestMapp
ingHandlerMapping : Mapped "{[/superHero],methods=[GET]}" onto public org.spring
framework.http.ResponseEntity<com.ga1.superheroes.repository.RepositorySuperHero
> com.ga1.superheroes.MainController.getSuperHero(java.lang.String)
2016-02-13 23:57:52.640  INFO 7804 --- [ost-startStop-1] s.w.s.m.m.a.RequestMapp
ingHandlerMapping : Mapped "{[/superHeroes],methods=[GET]}" onto public org.spri
ngframework.http.ResponseEntity<java.util.List<java.lang.String>> com.ga1.superh
eroes.MainController.getSuperHeroes()
2016-02-13 23:57:52.641  INFO 7804 --- [ost-startStop-1] s.w.s.m.m.a.RequestMapp
ingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public org.springfram
ework.http.ResponseEntity<java.lang.String> com.ga1.superheroes.MainController.h
ello()
2016-02-13 23:57:52.654  INFO 7804 --- [ost-startStop-1] s.w.s.m.m.a.RequestMapp
ingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.Res
ponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframewo
rk.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServ
letRequest)
2016-02-13 23:57:52.655  INFO 7804 --- [ost-startStop-1] s.w.s.m.m.a.RequestMapp
ingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.spr
ingframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web
.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.serv
let.http.HttpServletResponse)
2016-02-13 23:57:52.733  INFO 7804 --- [ost-startStop-1] o.s.w.s.handler.SimpleU
rlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class or
g.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-13 23:57:52.733  INFO 7804 --- [ost-startStop-1] o.s.w.s.handler.SimpleU
rlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.spring
framework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-13 23:57:52.843  INFO 7804 --- [ost-startStop-1] o.s.w.s.handler.SimpleU
rlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [clas
s org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-13 23:57:54.877  INFO 7804 --- [ost-startStop-1] org.mongodb.driver.clus
ter               : Cluster created with settings {hosts=[localhost:27017], mode
=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWait
QueueSize=500}
2016-02-13 23:57:55.325  INFO 7804 --- [localhost:27017] org.mongodb.driver.conn
ection            : Opened connection [connectionId{localValue:1, serverValue:16
}] to localhost:27017
2016-02-13 23:57:55.328  INFO 7804 --- [localhost:27017] org.mongodb.driver.clus
ter               : Monitor thread successfully connected to server with descrip
tion ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED
, ok=true, version=ServerVersion{versionList=[3, 0, 5]}, minWireVersion=0, maxWi
reVersion=3, maxDocumentSize=16777216, roundTripTimeNanos=876166}
2016-02-13 23:57:55.549  INFO 7804 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBe
anExporter        : Registering beans for JMX exposure on startup
2016-02-13 23:57:55.590  INFO 7804 --- [ost-startStop-1] com.ga1.SuperHeroesAppl
ication           : Started SuperHeroesApplication in 11.432 seconds (JVM runnin
g for 24.405)
13-Feb-2016 23:57:55.716 INFO [localhost-startStop-1] org.apache.catalina.startu
p.HostConfig.deployWAR Deployment of web application archive C:\Program Files\Ap
ache Software Foundation\Tomcat 8.0\webapps\superheroes-0.0.1-SNAPSHOT.war has f
inished in 22,750 ms
13-Feb-2016 23:57:55.721 INFO [main] org.apache.coyote.AbstractProtocol.start St
arting ProtocolHandler ["http-nio-8080"]
13-Feb-2016 23:57:55.737 INFO [main] org.apache.coyote.AbstractProtocol.start St
arting ProtocolHandler ["ajp-nio-8009"]
13-Feb-2016 23:57:55.742 INFO [main] org.apache.catalina.startup.Catalina.start
Server startup in 22911 ms

Below are my: pom, controller and main files

<?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.ga1</groupId>
    <artifactId>superheroes</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>superheroes</name>
    <description>really super</description>

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

    <properties>
<!--        <tomcat.version>8.0.8</tomcat.version> -->
<!--        <start-class>com.ga1.SuperHeroesApplication</start-class> -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.2.1</version>
        </dependency>
    </dependencies>

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


</project>

The controller:

package com.ga1.superheroes;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ResponseEntity<String> hello() {
        return new ResponseEntity<>("Hello World", HttpStatus.OK);
    }

}

and the Main:

package com.ga1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class SuperHeroesApplication extends SpringBootServletInitializer {

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

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

}

Upvotes: 0

Views: 7918

Answers (1)

pczeus
pczeus

Reputation: 7868

I think the issue is with the URL you are using. Because you are now deploying on a standalone Tomcat, your war will be deployed under a context that commonly matches the war file name, unless you specify the context in the application.properties. Can you provide your application.properties.

You can set the context in application.properties this way:

server.contextPath=/<whatever>

Otherwise, I'm betting your URL would be accessed like this as is:

/superheroes-0.0.1-SNAPSHOT/hello

Upvotes: 3

Related Questions