gpt4 book ai didi

spring-boot - 将spring boot项目打包部署成war success with profile,但是访问页面时无法访问页面

转载 作者:行者123 更新时间:2023-11-28 23:11:44 27 4
gpt4 key购买 nike

在我从 gradle 构建成功获取 war 文件后,我部署到 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@ 并且还有 application-dev .propertiessrc\main\resource 文件夹中。我生成war文件的方式是./gradlew -PactiveProfiles=dev clean build,生成war文件后,我把它放在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:\IDEA\PHOENIX\SRC
├───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中,它有简单的 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)

实际结果:当我访问该页面时,我得到 This site can't be reached via Tomcat is running properly

更新:这是 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:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\phoenix-0.0.1\WEB-INF\classes started by zhibo in C:\Users\zhibo\Desktop\apache-tomcat-9.0.22\bin)
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:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\phoenix-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:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\docs]
16-Jul-2019 14:27:48.528 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\docs] 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:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\examples]
16-Jul-2019 14:27:48.751 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\examples] 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:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\host-manager]
16-Jul-2019 14:27:48.780 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\host-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:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\manager]
16-Jul-2019 14:27:48.797 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\manager] 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:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\ROOT]
16-Jul-2019 14:27:48.812 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\ROOT] 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 上启动我的页面。

最佳答案

来自您的日志:

Deployment of web application archive [C:\Users\zhibo\Desktop\apache-tomcat-9.0.22\webapps\phoenix-0.0.1.war] has finished in [4,388] ms

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

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

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

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

关于spring-boot - 将spring boot项目打包部署成war success with profile,但是访问页面时无法访问页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57063283/

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