Spring Boot學習筆記(四)構建RESTful API標準工程例項

本文主要記錄搭建RESTful API標準工程,包含比較推薦的工程結構,掌握一些基本註解,並引入Swagger

新建一個專案

透過

Spring Initializr

建立工程,選擇所需要的jar包,如下圖:

Spring Boot學習筆記(四)構建RESTful API標準工程例項

Spring Boot學習筆記(四)構建RESTful API標準工程例項

Spring Boot學習筆記(四)構建RESTful API標準工程例項

工程結構

Spring Boot框架對工程結構並沒有什麼特殊的限制,我這邊基本按照網上主流和自己一些喜好進行劃分,供參考:

程式碼層結構

前端控制器(Controller):

com.xxx.controller

資料服務層(Service):

com.xxx.service

實體(Entity)與資料訪問層(Repository):

com.xxx.domain

公共方法及工具類:

com.xxx.common

資原始檔的結構

配置檔案:

src/main/resources/config

靜態檔案:

src/main/resources/static

模板:

src/main/resources/templates

Spring Boot學習筆記(四)構建RESTful API標準工程例項

編寫第一個服務在

controller

下新建類

HelloController

,相關程式碼做了簡單的註釋,如下:

//相當於 @Controller + @ResponseBody//該註解 方法method 返回型別是String時候則返回string,返回物件時候則講json_encode 該物件的json字串@RestControllerpublic class HelloController { //該註解mapping指定路由 @RequestMapping(value = “/hello”,method = RequestMethod。GET) public String SayHello() { return “Hello Spring Boot”; }}

編寫到這裡,已經可以直接編譯運行了,這裡值得注意的是被

@SpringBootApplication

註解的啟動類一定要放在所有的RestController的根路徑的package下,@SpringBootApplication只會掃描@SpringBootApplication註解標記類包下及其子包的類,如果不放在根路徑下,可以指定下:

@SpringBootApplication(scanBasePackages = “com。example。api_demo”)

無需配置Tomcat,直接啟動,輸入對應的地址可看到結果:

Spring Boot學習筆記(四)構建RESTful API標準工程例項

Spring Boot學習筆記(四)構建RESTful API標準工程例項

增加service層

在搭建了基礎應用的基礎上,我們增加service層抽離控制層和業務層程式碼。

service

下新增

HelloService

HelloServiceImpl

兩個類,程式碼如下:

//業務層介面:HelloServicepublic interface HelloService { public String sayHello();}//介面實現:HelloServiceImpl@Servicepublic class HelloServiceImpl implements HelloService{ @Override public String sayHello() { return “Hello Spring Boot”; }}

修改對應的controller程式碼:

@Autowiredprivate HelloService helloService;@RequestMapping(value = “/hello”,method = RequestMethod。GET)public String sayHello(){ return helloService。sayHello();}

這樣,簡單的拆分就完成了。

引入Swagger

Swagger是什麼大家自行百度,對於Restful API來說,Swagger絕對是它的好基友。

首先引入對應的jar包,在

pom。xml

加入:

io。springfox springfox-swagger2 2。7。0 io。springfox springfox-swagger-ui 2。7。0

新增類

Swagger2。java

,程式碼如下:

@Configuration@EnableSwagger2public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType。SWAGGER_2) 。apiInfo(apiInfo()) 。select() 。apis(RequestHandlerSelectors。basePackage(“com。example。api_demo。controller”)) 。paths(PathSelectors。any()) 。build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() 。title(“測試文件”) 。description(“這裡是一段描述”) 。termsOfServiceUrl(“http://www。bug2048。com/”) 。version(“1。0”) 。build(); }}

到這裡,Swagger就算配置完成了,接下來就是要新增文件內容了,修改之前的

HelloController

之後,直接編譯執行,透過

/swagger-ui。html

就能看到效果

@ApiOperation(value=“增加Service層輸出Hello”, notes=“這是第二個demo”)@RequestMapping(value = “/hello”,method = RequestMethod。GET)public String sayHello(){ return helloService。sayHello();}

Spring Boot學習筆記(四)構建RESTful API標準工程例項

Spring Boot學習筆記(四)構建RESTful API標準工程例項

至此,簡單的框架算是完成了,後面就可以逐步完善,包括接入日誌,資料庫等等,後面會持續更新。

總結

基於Spring Boot構建RESTful API相對來說還是比較便捷的,其中註解使得程式碼更加簡潔,本次用到註解再彙總下,有時間的話可以深入理解下其背後的原理:

@SpringBootApplication

: 申明讓spring boot自動給程式進行必要的配置。

@RestController

:REST風格的控制器

@RequestMapping

:提供路由資訊,負責URL到Controller中的具體函式的對映

@Service

:一般用於修飾service層的元件

@Autowired

: 自動匯入依賴的bean