1. 程式人生 > >Spring Boot下的第一個應用程式

Spring Boot下的第一個應用程式

簡單的一個功能:瀏覽器傳送hello請求,伺服器收到請求並進行處理,響應helloworld字串
1:建立一個maven工程(jar)
2:匯入springboot相關的依賴

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3編寫一個主程式:啟動springboot應用

/*
*SpringBootApplication用來標註一個主程式類,說明這是一個springboot應用
*/
@SpringBootApplication
public class HelloWorld {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorld.class,args);
    }
}

4編寫相關的controller,service

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }
}

5執行主程式
6簡化部署(不再需要打war包)