【Android免費音樂下載app】【佳語音樂下載】建議最少2.0.3版本。最新版本:
https://gitlab.com/gaopinqiang/checkversion/raw/master/Music_Download.apk
一、認識 Restful
Restful
一種互聯網軟件架構設計的風格,但它並不是標準,它只是提出了一組客戶端和服務器交互時的架構理念和設計原則,基於這種理念和原則設計的接口可以更簡潔,更有層次;
REST這個詞,是Roy Thomas Fielding在他2000年的博士論文中提出的;
如果一個架構符合REST原則,就稱它爲Restful架構;
比如:
我們要訪問一個http接口:http://localhost:80080/api/order?id=1521&status=1
採用Restful風格則http地址爲:http://localhost:80080/api/order/1021/1
二、SpringBoot開發Restful
Spring boot開發Restful 主要是幾個註解實現:
-
1、@PathVariable(最重要)
獲取url中的數據;該註解是實現Restful最主要的一個註解;
-
2、增加 post方法
PostMapping;接收和處理Post方式的請求;
-
3、刪除 delete方法
DeleteMapping;接收delete方式的請求,可以使用GetMapping代替;
-
4、修改 put方法
PutMapping;接收put方式的請求,可以用PostMapping代替;
-
5、查詢 get方法
GetMapping;接收get方式的請求;
示例演示:
在controller包下創建一個RestfulController
package com.springboot.web.controller;
import com.springboot.web.model.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestfulController {
@RequestMapping("/user/{id}")
public Object getUser(@PathVariable("id") Integer id){
System.out.println("id = " + id );
User user = new User();
user.setId(id);
return user;
}
@RequestMapping("/user/{id}/{name}")
public Object getUser(@PathVariable("id") Integer id,@PathVariable("name") String name){
System.out.println("id = " + id + " || name = " + name);
User user = new User();
user.setName(name);
user.setId(id);
return user;
}
}
運行截圖如下:
注意:如果@RequestMapping中的參數個數和類型一樣,但是順序不一樣。請求會出現問題。
比如:@RequestMapping("/user/{id}/{age}") 和 @RequestMapping("/user/{age}/{id}") ,會報一個下面的異常。
java.lang.IllegalStateException: Ambiguous handler methods mapped for ‘/user/123/12’: {public java.lang.Object com.springboot.web.controller.RestfulController.getUser(java.lang.Integer,java.lang.String), public java.lang.Object com.springboot.web.controller.RestfulController.getUser2(java.lang.Integer,java.lang.Integer)}
三、SpringBoot 熱部署插件(spring-boot-devtools)
在實際開發中,我們修改某些代碼邏輯功能或頁面都需要重啓應用,這無形中降低了開發效率;
熱部署是指當我們修改代碼後,服務能自動重啓加載新修改的內容,這樣大大提高了我們開發的效率;
Spring boot熱部署通過添加一個插件實現;
插件爲:spring-boot-devtools,在Maven中配置如下:
<!-- springboot 開發自動熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
該熱部署插件在實際使用中會有一些小問題,明明已經重啓,但沒有生效,這種情況下,手動重啓一下程序;