简体   繁体   English

没有错误,但是映射不起作用Spring Boot + tomcat 8.0

[英]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. 当我将其打包到jar文件中时,该应用程序可以在本地计算机上顺利运行。 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 ) 我决定将其部署在AWS Elastic beantalk上,因此我做了创建.war文件所需的更改( https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment .html

I wanted to try it running on my local TOMCAT. 我想尝试在我的本地TOMCAT上运行它。 So i placed the war file in the webapps directory, and run the Tomcat 8.0\\bin\\Tomcat8.exe 因此,我将war文件放置在webapps目录中,并运行Tomcat 8.0 \\ bin \\ Tomcat8.exe

The logs in the console look ok, there is even an info that the /hello route got mapped. 控制台中的日志看起来不错,甚至还有/hello路由已映射的信息。 The server runs, but when I enter the localhost:8080/hello nothing is returned. 服务器运行,但是当我输入localhost:8080 / hello时,什么都没有返回。 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 下面是我的: pomcontrollermain文件

<?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);
    }

}

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. 由于您现在正在独立的Tomcat上进行部署,因此除非您在application.properties中指定上下文,否则将在通常与war文件名匹配的上下文中部署war。 Can you provide your application.properties. 您能提供您的application.properties吗?

You can set the context in application.properties this way: 您可以通过以下方式在application.properties中设置上下文:

server.contextPath=/<whatever>

Otherwise, I'm betting your URL would be accessed like this as is: 否则,我敢打赌,您的网址将按以下方式访问:

/superheroes-0.0.1-SNAPSHOT/hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM