使用配置文件将 spring boot 项目打包并部署到战争成功中,但在我尝试访问该页面时无法访问该页面



在我成功从gradle构建中获取战争文件后,我部署到tomcat容器,这也成功了。但是当我试图访问localhost:9999/fdr/test时,它告诉我This site can’t be reached

我使用的Tomcat版本是apache-tomcat-9.0.22。我已经更改了build.gradle文件以dependencies部分下添加providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'。同样为了让Tomcat知道我使用的配置文件,我在application.properties中添加了spring.profiles.active=@activeProfiles@,并在srcmainresource文件夹中添加了application-dev.properties。我生成战争文件的方式是./gradlew -PactiveProfiles=dev clean build,一旦生成了战争文件,我就把它放在Tomcat下的webapps文件夹中,然后启动它

对于build.gradle

plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
id 'war'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.vincent.wartest'
version = '0.0.1'
sourceCompatibility = '1.8'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
processResources {
filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
activeProfiles: activeProfiles
]
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation('mysql:mysql-connector-java:8.0.13')
}

结构如下所示:

V:IDEAPHOENIXSRC
├───main
│   ├───java
│   │   └───com
│   │       └───vincent
│   │           └───wartest
│   │               └───phoenix
│   │                   │   lombok.config
│   │                   │   PhoenixApplication.java
│   │                   │   ServletInitializer.java
│   │                   │
│   │                   └───controller
│   │                           PhoenixController.java
│   │
│   └───resources
│       │   application-dev.properties
│       │   application.properties
│       │
│       ├───static
│       └───templates
└───test
└───java
└───com
└───vincent
└───wartest
└───phoenix
PhoenixApplicationTests.java

由于代码是由 Intellij 自动生成的,因此它创建了一个名为ServletInitializer.java的类,如下所示

package com.vincent.wartest.phoenix;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(PhoenixApplication.class);
}
}

com.vincent.wartest.controller中,它有简单的控制器类


import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/fdr")
@Slf4j
public class PhoenixController {
@GetMapping(value="/test")
public ResponseEntity<String> test(){
LOGGER.info("Logger test");
return new ResponseEntity<>("First test", HttpStatus.OK);
}
}

预期结果:在 tomcat 上部署后,当我访问http://localhost:9999/fdr/test时,页面应该显示First test,响应代码为 200 OK(通过检查 Chrome 上的 F12)

实际结果:当我访问该页面时,我通过TomcatThis site can’t be reached运行正常

更新:这是登录catalina.out

.   ____          _            __ _ _
/\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v2.1.6.RELEASE)
2019-07-16 14:27:46.447  INFO 28080 --- [           main] com.itsp.fdr.phoenix.ServletInitializer  : Starting ServletInitializer on hospt1 with PID 28080 (C:UserszhiboDesktopapache-tomcat-9.0.22webappsphoenix-0.0.1WEB-INFclasses started by zhibo in C:UserszhiboDesktopapache-tomcat-9.0.22bin)
2019-07-16 14:27:46.450  INFO 28080 --- [           main] com.itsp.fdr.phoenix.ServletInitializer  : The following profiles are active: dev
2019-07-16 14:27:47.546  INFO 28080 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1037 ms
2019-07-16 14:27:48.074  INFO 28080 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-16 14:27:48.437  INFO 28080 --- [           main] com.vincent.wartest.phoenix.ServletInitializer  : Started ServletInitializer in 2.559 seconds (JVM running for 5.498)
16-Jul-2019 14:27:48.481 INFO [main] 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 scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
16-Jul-2019 14:27:48.508 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:UserszhiboDesktopapache-tomcat-9.0.22webappsphoenix-0.0.1.war] has finished in [4,388] ms
16-Jul-2019 14:27:48.509 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappsdocs]
16-Jul-2019 14:27:48.528 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappsdocs] has finished in [19] ms
16-Jul-2019 14:27:48.528 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappsexamples]
16-Jul-2019 14:27:48.751 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappsexamples] has finished in [223] ms
16-Jul-2019 14:27:48.751 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappshost-manager]
16-Jul-2019 14:27:48.780 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappshost-manager] has finished in [28] ms
16-Jul-2019 14:27:48.780 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappsmanager]
16-Jul-2019 14:27:48.797 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappsmanager] has finished in [16] ms
16-Jul-2019 14:27:48.797 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappsROOT]
16-Jul-2019 14:27:48.812 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UserszhiboDesktopapache-tomcat-9.0.22webappsROOT] has finished in [15] ms
16-Jul-2019 14:27:48.815 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
16-Jul-2019 14:27:48.823 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
16-Jul-2019 14:27:48.826 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [4,736] milliseconds

它实际上加载了我server.port=9999配置的开发配置文件,所以我希望在端口 9999 上它会启动我的页面。

从您的日志中:

Web 应用程序归档 [C:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\phoenix-0.0.1.war] 的部署已在 [4,388] 毫秒内完成

在 tomcat 上,应用程序根上下文以战争名称命名,在本例中为:phoenix-0.0.1. 所以你的申请将在

http://localhost:9999/phoenix-0.0.1/fdr/test

如果要将版本保留在上下文路径中,则最好将战争命名为phoenix##0.0.1.war。 所以你的申请将在

http://localhost:9999/phoenix/0.0.1/fdr/test

最新更新