IDEA打包war部署到tomcat 404错误(tomcat配置正确)

IDEA打包war部署到tomcat 404错误(tomcat配置正确)

此问题状况可能满足以下条件的

1.本地使用gradle bootRun后访问地址正常
2.部署到tomcat后访问接口地址出现404错误
3.在tomcat项目中新建index.html可以访问

解决方案

1.gradle首先确认满足以下条件


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

apply plugin: 'war'
apply plugin: 'org.springframework.boot'
...

war {
    baseName = 'projectname'
    version =  '0.0.1'
}

repositories {
    mavenCentral()
    jcenter()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")

    //注意打包war此处是providedRuntime,不是compile
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")

    ...
}

2.Application.java文件需要修改成如下,用来告诉容器入口


@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

之前我这边错误样例 如下,bootRun可以跑,但是tomcat就不行


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

参考

https://stackoverflow.com/questions/25884010/gradle-spring-boot-project-not-working-in-tomcat-as-a-war

你可能感兴趣的:(坑)