gpt4 book ai didi

java - 找不到 Spring Tomcat 404 资源

转载 作者:行者123 更新时间:2023-11-28 22:50:52 24 4
gpt4 key购买 nike

我正在尝试在 Tomcat 安装中使用 Spring 部署一个简单的 REST api,但我得到的只是部署 war 时未找到 404 资源。它在本地主机上运行完美,但部署就是行不通。尝试了 stackoverflow 上的每个解决方案,但几乎都使用配置文件而不是配置注释。这是我的(重要的)代码。

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

@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

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

最佳答案

SpringBootServletInitializer

您的代码示例仅显示了一个 main 方法。部署为 WAR 时,不会执行 main。您需要扩展 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);
}
}

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

顺便说一句,为什么你使用 AnnotationConfigApplicationContext 来注册 AppConfig 而不是让 Spring Boot 获取它?要么:

  • AppConfig 与您的 SlemberApplication 位于同一包(或下方):默认加载;
  • 移动 SlemberApplication 使其(在包中)高于所有 @Configuration/@Component 类(然后前一点将适用);<
  • 或者您添加 @ComponentScan 以指定 AppConfig 的包。

你可以使用这样的东西:

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

关于java - 找不到 Spring Tomcat 404 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966012/

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