gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:41:47 32 4
gpt4 key购买 nike

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

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