copy
到banner.txt
,便可替換Spring Boot
的開啓圖標88 88
88 88
88 88
88 88 ,adPPYba, 8b d8 ,adPPYba, 88 88
88 88 a8" "8a `8b d8' a8P_____88 88 88 88 88 8b d8 `8b d8' 8PP""""""" 88 88 88 88 "8a, ,a8" `8b,d8' "8b, ,aa "8a, ,a88 88 88 `"YbbdP"' "8" `"Ybbd8"' `"YbbdP'Y8 88 88 88 88 "" "" 88 ,adPPYba, 88,dPPYba, 88 88,dPYba,,adPYba, 88 8b,dPPYba, ,adPPYb,d8 I8[ "" 88P' "8a 88 88P' "88" "8a 88 88P' `"8a a8" `Y88 `"Y8ba, 88 88 88 88 88 88 88 88 88 8b 88 aa ]8I 88 88 88 88 88 88 88 88 88 "8a, ,d88 `"YbbdP"' 88 88 88 88 88 88 88 88 88 `"YbbdP"Y8
aa, ,88
"Y8bbdP"
複製代碼
Swagger
是一款能夠快速生成符合RESTful
風格API
並進行在線調試的插件。REST
實際上爲Representational State Transfer
的縮寫,翻譯爲「表現層狀態轉化」 。若是一個架構符合REST
原則,就稱它爲RESTful
架構。 實際上,「表現層狀態轉化」省略了主語,完整的說應該是「資源表現層狀態轉化」。什麼是資源Resource
?資源指的是網絡中信息的表現形式,好比一段文本,一首歌,一個視頻文件等等;什麼是表現層Reresentational
?表現層即資源的展示在你面前的形式,好比文本能夠是JSON
格式的,也能夠是XML
形式的,甚至爲二進制形式的。圖片能夠是gif
,也能夠是PNG
;什麼是狀態轉換State Transfer
?用戶可以使用URL
經過HTTP
協議來獲取各類資源,HTTP
協議包含了一些操做資源的方法,好比:GET
用來獲取資源, POST
用來新建資源 , PUT
用來更新資源,DELETE
用來刪除資源,PATCH
用來更新資源的部分屬性。經過這些HTTP協議的方法來操做資源的過程即爲狀態轉換。html
URL
請求和RESTful
風格請求的區別描述 傳統請求 方法 RESTful請求 方法
查詢 /user/query?name=mrbird GET /user?name=mrbird GET
詳情 /user/getInfo?id=1 GET /user/1 GET
建立 /user/create?name=mrbird POST /user POST
修改 /user/update?name=mrbird&id=1 POST /user/1 PUT
刪除 /user/delete?id=1 GET /user/1 DELETE
複製代碼
RESTful
請求的幾個區別:create
,delete
等;RESTful
請求經過URL
來描述資源。RESTful
請求經過HTTP
請求的方法來描述行爲,好比DELETE
,POST
,PUT
等,而且使用HTTP
狀態碼來表示不一樣的結果。RESTful
請求經過JSON
來交換數據。<!-- MySQL 鏈接驅動依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Spring Boot Mybatis 依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!--使用的Swagger版本爲2.6.1:-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<!--ui的界面-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
複製代碼
/**
* author: Created by shiming on 2018/9/26 18:10
* mailbox:lamshiming@sina.com
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
// 在配置類中添加@EnableSwagger2註解來啓用Swagger2,apis()定義了掃描的包路徑
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("RESTful API 文檔")
.contact(new Contact("shiming", "https://www.shiming.site/", null))
.version("1.0")
.build();
}
}
複製代碼
## 數據源配置
spring.datasource.url= jdbc:mysql://localhost:3306/shiming?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=App123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
複製代碼
建立數據庫java
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
`user_name` varchar(25) DEFAULT NULL COMMENT '用戶姓名',
`user_age` varchar(25) DEFAULT NULL COMMENT '用戶年級',
`description` varchar(25) DEFAULT NULL COMMENT '用戶描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
複製代碼
INSERT user VALUES (0,'仕明','25','仕明是個好同窗');
複製代碼
最終的建立的表 mysql
訪問本地地址 :http://localhost:8080/swagger-ui.html#/git
使用 swagger
新增用戶 github
使用 swagger
獲取用戶列表,也能夠訪問:http://localhost:8080/user/listweb
{
"code": "0",
"message": "success",
"result": [
{
"id": 1,
"name": "仕明",
"age": "25",
"description": "仕明是個好同窗"
},
{
"id": 2,
"name": "仕明",
"age": "25",
"description": "仕明是個好同窗"
},
{
"id": 3,
"name": "仕明",
"age": "25",
"description": "仕明是個好同窗"
},
{
"id": 4,
"name": "仕明",
"age": "25",
"description": "仕明是個好同窗"
},
{
"id": 5,
"name": "仕明",
"age": "25",
"description": "仕明是個好同窗"
},
{
"id": 6,
"name": "仕明",
"age": "25",
"description": "仕明是個好同窗"
},
{
"id": 7,
"name": "仕明",
"age": "25",
"description": "仕明是個好同窗"
}
]
}
複製代碼
使用 swagger
刪除id=1
的用戶! spring
使用 swagger
獲取id=2
的用戶! sql
{
"code": "0",
"message": "success",
"result": {
"id": 2,
"name": "仕明",
"age": "25",
"description": "仕明是個好同窗"
}
}
複製代碼
swagger
更新用戶!{
"id": 7,
"name": "仕明",
"age": "25",
"description": "我更新了-仕明是個好同窗"
}
複製代碼
更新後的數據庫 數據庫
UserController的代碼以下api
/**
* author: Created by shiming on 2018/9/26 16:42
* mailbox:lamshiming@sina.com
*/
@Api(value = "用戶Controller")
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
UserService cityService;
//{"id":1,"name":"仕明","age":"25","description":"仕明是個好同窗"}
// http://localhost:8080/user/1
@ApiOperation(value = "獲取用戶信息", notes = "根據用戶id獲取用戶信息")
@ApiImplicitParam(name = "id", value = "用戶id", required = true, dataType = "Long", paramType = "path")
@GetMapping("/{id}")
@ResponseBody
public ResultBody getUserById(@PathVariable(value = "id") Long id) throws GlobalErrorInfoException {
System.out.println("id="+id);
User userById = cityService.getUserById(id);
if(userById!=null){
ResultBody resultBody = new ResultBody(userById);
return resultBody;
}
// User user = new User();
// user.setDescription("沒有找到這我的");
throw new GlobalErrorInfoException(GlobalErrorInfoEnum.NOT_FOUND);
// return user;
}
// http://localhost:8080/user/list
@ApiOperation(value = "獲取用戶列表", notes = "獲取用戶列表")
@GetMapping("/list")
@ResponseBody
public ResultBody getUserList() throws GlobalErrorInfoException {
List<User> userList = cityService.getUserList();
if (userList==null||userList.size()==0){
throw new GlobalErrorInfoException(GlobalErrorInfoEnum.NOT_FOUND);
}
ResultBody resultBody = new ResultBody(userList);
return resultBody;
}
@ApiOperation(value = "新增用戶", notes = "根據用戶實體建立用戶")
@ApiImplicitParam(name = "user", value = "用戶實體", required = true, dataType = "User")
@PostMapping("/add")
@ResponseBody
public ResultBody addUser(@RequestBody User user) {
Long aLong = cityService.addUser(user);
System.out.println("Long=="+aLong);
Map<String, Object> map = new HashMap<>();
map.put("result", "新增用戶成功");
ResultBody resultBody = new ResultBody(map);
return resultBody;
}
@ApiOperation(value = "刪除用戶", notes = "根據用戶id刪除用戶")
@ApiImplicitParam(name = "id", value = "用戶id", required = true, dataType = "Long", paramType = "path")
@DeleteMapping("/{id}")
@ResponseBody
public ResultBody deleteUser(@PathVariable(value = "id") Long id) {
Long aLong = cityService.deleteUser(id);
System.out.println("along="+aLong);
System.out.println("刪除掉的id="+id);
Map<String, Object> map = new HashMap<>();
map.put("result", "刪除成功");
ResultBody resultBody = new ResultBody(map);
return resultBody;
}
@ApiOperation(value = "更新用戶", notes = "根據用戶id更新用戶")
@ApiImplicitParams(@ApiImplicitParam(name = "user", value = "用戶實體", required = true, dataType = "User"))
@PutMapping("/{id}")
@ResponseBody
public ResultBody updateUser(@RequestBody User user) {
System.out.println(user.toString());
Long aLong = cityService.updateUser(user);
System.out.println("aLong="+aLong);
Map<String, Object> map = new HashMap<>();
map.put("result", "更新成功");
ResultBody resultBody = new ResultBody(map);
return resultBody;
}
}
複製代碼
@Api:修飾整個類,描述Controller的做用;
@ApiOperation:描述一個類的一個方法,或者說一個接口;
@ApiParam:單個參數描述;
@ApiModel:用對象來接收參數;
@ApiProperty:用對象接收參數時,描述對象的一個字段;
@ApiResponse:HTTP響應其中1個描述;
@ApiResponses:HTTP響應總體描述;
@ApiIgnore:使用該註解忽略這個API;
@ApiError :發生錯誤返回的信息;
@ApiImplicitParam:一個請求參數;
@ApiImplicitParams:多個請求參數。
編寫RESTful API接口
本文git地址Swagger2Demo
最後說明幾點
RESTful
只是一種風格,並非一種強制性的標準。Swagger UI
spring.profiles.active=dev
使用的是開發的環境ResultBody
,來返回給客戶端,若是不寫getXXX
的方法的話,IllegalArgumentException
: 不合法的參數異常。No converter found for return value of type
: 找不到類型返回值的轉換器 .這個類必定得提供。