我熟悉使用servlet映射Spring的舊方法,但我想我會嘗試SpringBoot爲新應用程序更快地開始並儘可能使用默認值。SpringBoot helloworld MVC應用程序將不會加載
我似乎無法得到最基本的helloworld控制器工作。
看來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'
}
Application.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);
}
}
和控制器:
@RestController
@EnableAutoConfiguration
public class IndexController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
如果你部署它,main將不會運行。爲此只需運行它作爲Java應用程序。 – 2015-04-01 11:44:51
謝謝。我認爲那就是它。所有的例子都是用捆綁的應用服務器運行jar。我真的想建立一個可以部署的戰爭。 SpringBoot能夠像普通的戰爭一樣工作嗎? – 2015-04-01 11:49:05
其實根據的「包裝可執行的JAR和WAR文件」的文檔我與配置{ providedRuntime }等,所以我認爲主要的方法將不會被使用得到正確的配置,但我還是什麼都看不到發生。我的根URL提供了一個404,並嘗試更改application.properties中的日誌配置完全沒有影響。 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-gradle-packaging – 2015-04-01 11:51:58