首页 > 解决方案 > 使用profile打包部署spring boot项目成war成功,但是尝试访问页面时无法访问页面

问题描述

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

我使用的 Tomcat 版本是 apache-tomcat-9.0.22。我已经将 build.gradle 文件更改为providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'dependencies部分下添加。另外为了让 Tomcat 知道我使用的配置文件,我添加spring.profiles.active=@activeProfiles@application.properties并且也有application-dev.propertiesinsrc\main\resource文件夹。我生成war文件的方法是./gradlew -PactiveProfiles=dev clean build,一旦生成war文件,我将它放在webappsTomcat下的文件夹中,然后启动它

为了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,它有简单的控制器类


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通过 Tomcat 得到的运行正常

更新: 这是登录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 上启动我的页面。

标签: spring-boottomcat

解决方案


从您的日志中:

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


推荐阅读