我熟悉将 spring 与 servlet 映射一起使用的旧方法,但我想我会尝试将 SpringBoot 用于新应用程序以加快速度并尽可能使用默认值。
我似乎无法让最基本的 helloworld Controller 工作。
似乎 SpringBoot 根本没有引导。我在 Application main 方法中放置了一个断点,但它没有中断。
使用gradle构建项目,通过IntelliJ部署到tomcat中。
我确定我错过了一些非常简单的东西。但它是什么?
这是 gradle 文件:
buildscript {
ext {
springBootVersion = '1.2.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
war {
baseName = 'test'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-log4j2")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.12'
}
应用程序.java
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(TestApplication.class, args);
}
}
Servlet 初始化程序:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TestApplication.class);
}
}
和 Controller :
@RestController
@EnableAutoConfiguration
public class IndexController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
最佳答案
找到问题了。
我在没有 spring boot 的情况下启动了一个新应用程序。只是一个老式的基于 xml 的 spring 应用程序,使用这种方法我可以看到一个异常。它提示在一个 Java 版本中编译并在另一个版本中运行。我试图让它在 Java 8 中工作。
我已经切换回 Java 7,果然,SpringBoot 应用程序运行良好。
感谢 PaulINUK 的建议。如果在引导 SpringBoot 时出现问题,那么看不到异常并不是很好。
关于java - Spring Boot hello world MVC 应用程序无法加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389838/