1 pom.xml配置
<parent>
<!--
spring boot 父節(jié)點(diǎn)依賴,
引入這個(gè)之后相關(guān)的引入就不需要添加version配置,
spring boot會(huì)自動(dòng)選擇最合適的版本進(jìn)行添加。
-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2 Hello World
package com.tutorial.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class HelloWorld {
public static void main(String[] args){
ConfigurableApplicationContext context = SpringApplication.run(HelloWorld.class, args);
System.out.println(context);
}
@RequestMapping("/")
public String helloWorld(){
return "hello world";
}
}
注解說明:
@SpringBootApplication申明讓spring boot自動(dòng)給程序進(jìn)行必要的配置,等價(jià)于以默認(rèn)屬性使用@Configuration,@EnableAutoConfiguration和@ComponentScan
@RestController返回json字符串的數(shù)據(jù),直接可以編寫RESTFul的接口;
Paste_Image.png
注意到,此時(shí)按照之前的web項(xiàng)目習(xí)慣,訪問的時(shí)候沒有帶項(xiàng)目路徑,這是因?yàn)镾pring Boot默認(rèn)設(shè)置了,將項(xiàng)目路徑直接設(shè)為根路徑。可以自己配置項(xiàng)目路徑在application.properties中配置server.contextPath=/aaa,測試在訪問http://localhost:8080/aaa/