gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:41:03 26 4
gpt4 key购买 nike

我想创建一个在 spring boot 上运行的小应用程序。当我将它打包成一个 jar 文件时,应用程序可以在我的本地机器上顺利运行。我决定将它部署在 AWS elastic beanstalk 上,所以我做了创建 .war 文件所需的更改 (https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html)

我想尝试在我本地的 TOMCAT 上运行它。所以我把war文件放在webapps目录下,运行Tomcat 8.0\bin\Tomcat8.exe

控制台中的日志看起来不错,甚至有一条信息表明 /hello 路由已被映射。服务器运行,但是当我输入 localhost:8080/hello 时,没有返回任何内容。没有错误,只是一个空白页面。

...........

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

下面是我的: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>

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

}

和主要的:

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

}

最佳答案

我认为问题出在您使用的 URL 上。因为您现在是在独立的 Tomcat 上部署,所以您的 war 将部署在通常与 war 文件名匹配的上下文中,除非您在 application.properties 中指定了上下文。你能提供你的application.properties吗?

您可以这样在 application.properties 中设置上下文:

server.contextPath=/<whatever>

否则,我敢打赌您的 URL 将按原样访问:

/superheroes-0.0.1-SNAPSHOT/hello

关于java - 没有错误,但是映射不起作用 Spring boot + tomcat 8.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com