1. 程式人生 > >springboot-helloworld

springboot-helloworld

sco put cnblogs rom cts mod 日誌 src 分享

1使用idea創建springboot項目如下圖所示 並選擇web模塊

技術分享圖片

2,登錄springboot官網 http://projects.spring.io/spring-boot/ 引入相關依賴包如圖所示我們這裏是基於1.5.8

技術分享圖片

3.引入springboot核心包,開始我們的helloworld之旅,pom文件如圖所示

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cm.test.cn</groupId> <artifactId>springboot-test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-test</name> <description>Demo project for
Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--引入springboot-starter核心Spring Boot starter,包括自動配置支持,日誌和YAML--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--對web應用支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--測試使用--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--maven插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

4、編寫啟動類(註意啟動類一般放在包的最外包)因為啟動類只會掃描本報子包,同級包中的信息不然會報錯

package cm.test.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootTestApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootTestApplication.class, args);
    }
}

5、編寫controller

package cm.test.cn.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class SampleController {
    @RequestMapping("/hello")
    public String home() {

        return "Hello World!";
    }

}

此時我們啟動,這個啟動類並在瀏覽器窗口訪問如下圖所示一個簡單的hello,world就完成了,很簡單吧!

技術分享圖片

項目已上傳碼雲:https://gitee.com/wuhongpu/springboot-test.git

springboot-helloworld