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

时间:2019-07-16 18:18:38

标签: spring-boot tomcat

从gradle构建成功获取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.properties文件夹中也添加了src\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中,它具有简单的控制器类


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)

实际结果:当我访问该页面时,我通过Tomcat得到This 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:\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上它将启动我的页面。

1 个答案:

答案 0 :(得分:0)

从您的日志中:

  

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

在tomcat上,应用程序根上下文以war名称命名,在这种情况下为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