over 8 years ago
簡單說明一下Spring Boot這東西最主要目的就是為了節省配置專案的時間,隨著Spring4發佈後似乎也逐漸被看到了,可以利用他快速建立一個基於RESTful的Web專案喔
必要軟體
1.JDK
2.Eclipse
3.Maven
首先請設定好你Exlipse的Maven,不會設的由此去使用Maven管理WEB專案套件
再來隨便新增一個Maven專案如下
在pom.xml增加spring-boot套件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
新增一個控制器,裡面隨便寫當作要回一個汽車物件這樣
package test.springboot;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class SimpleController {
@RequestMapping(value="/hello",method=RequestMethod.GET)
@ResponseBody
public Object hello(){
Car car = new Car();
car.setType("NO1");
car.setColor("red");
return car;
}
}
在主程式中增加你的控制器
package test.springboot;
import org.springframework.boot.SpringApplication;
public class App
{
public static void main( String[] args )
{
SpringApplication.run(SimpleController.class, args);
}
}
接下來執行這支App.java
執行結果大概像這樣子,預設是聽8080
對http://localhost:8080/hello做個測試
是不是簡單到一個不行呢?
稍微看了一下,其實可以利用Spring-Boot寫完後轉出一般佈署用的War檔,就又變成WebAP下的應用了
資料來源
http://www.cnblogs.com/rollenholt/p/3693055.html
http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web/1.1.1.RELEASE
http://www.infoq.com/cn/news/2014/01/spring-boot
http://jinnianshilongnian.iteye.com/blog/1997192