java - 在tomcat中部署后无法访问spring boot应用程序

标签 java tomcat gradle spring-boot

我已经使用 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

我在 Controller 中的请求映射设置为

    @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);
    }
}

最佳答案

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

因此,当应用程序部署在 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?

关于java - 在tomcat中部署后无法访问spring boot应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36055729/

相关文章:

java - 调用电话以响应 Onclick

java - 使用 BATCH 从文件夹运行 WebApp

android - 构建所有ABI,但仅打包一部分

android - 如何找到 gradle 依赖 URL

android - 将gradle插件更新到2.0.0时出现错误

java - 将私钥存储在 ServletContext 中

java - 如何确定 Android Studio 布局中使用的宽度和高度以及重新创建此设计的可能方法?

java - JSONObject 使用单例

xml - Tomcat 6和7动态web项目eclipse

javascript - 在本地主机上保存文件