简体   繁体   English

找不到Spring Tomcat 404资源

[英]Spring Tomcat 404 resource not found

I'm trying to deploy an simple REST api with Spring inside a Tomcat installation, but all I'm getting are 404 resource not found when the war is deployed. 我正在尝试在Tomcat安装中使用Spring部署一个简单的REST api,但是我所得到的只是部署战争时找不到404资源。 It's running perfectly on localhost, but deploying is just not working. 它可以在localhost上完美运行,但是部署无法正常进行。 Tried every solution on stackoverflow, but almost all use configuration files instead of configuration annotations. 在stackoverflow上尝试了每种解决方案,但是几乎所有解决方案都使用配置文件而不是配置注释。 This is my (important) code. 这是我的(重要)代码。

slember.gradle slember.gradle

 buildscript {
    ext {
        springBootVersion = '1.4.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

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

springBoot{
    executable = true
}

war {
    baseName = 'slember'
}

jar {
    baseName = 'slember'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile 'org.hibernate:hibernate-core:5.0.9.Final'
    compile("com.h2database:h2")
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile 'javax.servlet:javax.servlet-api:3.1.0'
    compile 'org.javassist:javassist:3.15.0-GA'
    compile 'mysql:mysql-connector-java:5.1.31'
    compile 'commons-dbcp:commons-dbcp:1.4'
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compileOnly "org.projectlombok:lombok:1.16.10"
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")


}

SlemberApplication.java SlemberApplication.java

@SpringBootApplication
@EntityScan(basePackageClasses = {
        SlemberApplication.class
})
public class SlemberApplication {

    public static AnnotationConfigApplicationContext context;

    public static void main(String[] args) {

        SpringApplication.run(SlemberApplication.class, args);
        context = new AnnotationConfigApplicationContext();
        context.register(AppConfig.class);
        context.refresh();
    }
}

META-INF/MANIFEST.MF META-INF / MANIFEST.MF

Manifest-Version: 1.0
Main-Class: com.letscode.SlemberApplication

SpringBootServletInitializer SpringBootServletInitializer

Your code sample only shows a main method. 您的代码示例仅显示一种main方法。 When deploying as a WAR, the main isn't executed. 部署为WAR时,不会执行主程序。 You need to extend SpringBootServletInitializer : 您需要扩展SpringBootServletInitializer

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

source: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html 来源: http : //docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

BTW, why do you use AnnotationConfigApplicationContext to register AppConfig instead of letting Spring Boot picking it up ? 顺便说一句,为什么您使用AnnotationConfigApplicationContext来注册AppConfig而不是让Spring Boot来注册它? Either : 要么:

  • AppConfig is in the same package (or below) of your SlemberApplication : it's loaded by default; AppConfigSlemberApplication位于同一软件包(或以下软件包):默认情况下加载;
  • move SlemberApplication so that it is higher (in the packages) than all your @Configuration/@Component classes (and then the previous point would apply); 移动SlemberApplication ,使其(在软件包中)高于所有@Configuration/@Component类(然后适用上一点);
  • or you add @ComponentScan to specify AppConfig 's package. 或添加@ComponentScan以指定AppConfig的程序包。

You could use something like this: 您可以使用如下形式:

@ComponentScan(basePackageClasses={AppConfig.class, SlemberApplication.class})
@SpringBootApplication
public class SlemberApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        super.configure(builder);
    }
    public static void main(String[] args) {
        new SpringApplicationBuilder().build().run(SlemberApplication.class, args);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM