【问题标题】:Cannot access spring boot application after deployment in tomcat在tomcat中部署后无法访问spring boot应用程序
【发布时间】:2016-07-03 12:23:46
【问题描述】:

我使用 Spring boot 创建了一个 REST API。我使用 gradle 作为我的构建工具。我已经使用嵌入式容器测试了应用程序,它工作正常。我可以通过localhost:8080/foo/bar 访问我的应用程序。

现在我已经将它部署到tomcat,没有任何错误。

Mar 17, 2016 1:58:47 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /usr/local/apache-tomcat-7.0.65/webapps/foo.war
Mar 17, 2016 1:58:48 PM org.apache.catalina.startup.TldConfig execute
INFO: 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.
Mar 17, 2016 1:58:48 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /usr/local/apache-tomcat-7.0.65/webapps/foo.war has finished in 623 ms

但我现在无法访问localhost:8080/foo/bar 的应用程序。我可以看到 tomcat 正在运行localhost:8080

这是build.gradle 文件。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'war'

war {
    baseName = 'foo'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

configurations{
    providedRuntime
}

dependencies {
   compile 'org.springframework.boot:spring-boot-starter-web'
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
     compile 'com.google.code.gson:gson:2.6.2'
     compile 'org.springframework:spring-webmvc:4.2.5.RELEASE'
     compile 'org.json:json:20151123'

     providedRuntime 'javax.ws.rs:javax.ws.rs-api:2.0.1'
     providedRuntime 'javax.servlet:javax.servlet-api:3.1.0'
     providedRuntime 'javax.servlet:jstl:1.2'

     testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.11'
}

我有一个带有以下代码的application.properties 文件

server.context-path=/foo

并且我在控制器中的请求映射设置为

    @CrossOrigin
    @RequestMapping("/bar")
    public String bar(){
       // Code
    }

我尝试了post 中提到的内容,但这也不起作用。

@SpringBootApplication
public class FooApplication {

    public static void main(String[] args) {
        SpringApplication.run(FooApplication.class, args);
    }

    // Used when deploying to a standalone servlet container
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(FooApplication.class);
    }
}

【问题讨论】:

  • 为什么不使用嵌入式tomcat并创建可执行jar?
  • @SMA 我希望这个应用程序在服务器上运行。在这种情况下,我不确定将应用程序设为可执行 jar 是否会有所帮助
  • @ak31 它将更容易维护,因为可执行 jar 已准备好作为服务安装。
  • 检查this 在应用服务器中部署
  • @ak31 这是一个偏好问题。我的服务器上有几项服务,我希望能够像任何其他服务一样使用sudo service myProject.jar status/start/stop 启动/停止它们。另一方面,通过 war 文件进行部署可以更好地与持续集成工具集成。

标签: java tomcat gradle spring-boot


【解决方案1】:

要部署 Spring Boot 应用程序,您必须有一个扩展 org.springframework.boot.context.web.SpringBootServletInitializer 并覆盖 org.springframework.boot.context.web.SpringBootServletInitializer.configure(SpringApplicationBuilder application) 的配置类

因此,当应用程序部署在 JEE Servlet 容器中时,Spring Boot 应用程序将被正确部署和服务。

示例

@Configuration
public class AppServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

Application.class 与 main 方法中 SpringApplication.run(Application.class, args); 的类相同

参考号:What about the Java EE Application Server?

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2021-11-06
    相关资源
    最近更新 更多