後臺寫接口,因爲要提供接口文檔給前臺使用,全部研究了一下swagger,看到網上有篇文章寫得不錯,就直接拿過來了。html
swagger用於定義API文檔。java
好處:git
一、項目結構github
和上一節同樣,沒有改變。web
二、pom.xmlspring
引入了兩個jar。後端
1
2
3
4
5
6
7
8
9
10
|
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>
2.2
.
2
</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>
2.2
.
2
</version>
</dependency>
|
三、Application.java api
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package
com.xxx.firstboot;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import
springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
//same as @Configuration+@EnableAutoConfiguration+@ComponentScan
@EnableSwagger2
//啓動swagger註解
public
class
Application {
public
static
void
main(String[] args) {
SpringApplication.run(Application.
class
, args);
}
}
|
說明:瀏覽器
引入了一個註解@EnableSwagger2四、UserController.javarestful
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package
com.xxx.firstboot.web;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.web.bind.annotation.RequestHeader;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.bind.annotation.RestController;
import
com.xxx.firstboot.domain.User;
import
com.xxx.firstboot.service.UserService;
import
io.swagger.annotations.Api;
import
io.swagger.annotations.ApiImplicitParam;
import
io.swagger.annotations.ApiImplicitParams;
import
io.swagger.annotations.ApiOperation;
import
io.swagger.annotations.ApiResponse;
import
io.swagger.annotations.ApiResponses;
@RestController
@RequestMapping
(
"/user"
)
@Api
(
"userController相關api"
)
public
class
UserController {
@Autowired
private
UserService userService;
// @Autowired
// private MyRedisTemplate myRedisTemplate;
@ApiOperation
(
"獲取用戶信息"
)
@ApiImplicitParams
({
@ApiImplicitParam
(paramType=
"header"
,name=
"username"
,dataType=
"String"
,required=
true
,value=
"用戶的姓名"
,defaultValue=
"zhaojigang"
),
@ApiImplicitParam
(paramType=
"query"
,name=
"password"
,dataType=
"String"
,required=
true
,value=
"用戶的密碼"
,defaultValue=
"wangna"
)
})
@ApiResponses
({
@ApiResponse
(code=
400
,message=
"請求參數沒填好"
),
@ApiResponse
(code=
404
,message=
"請求路徑沒有或頁面跳轉路徑不對"
)
})
@RequestMapping
(value=
"/getUser"
,method=RequestMethod.GET)
public
User getUser(
@RequestHeader
(
"username"
) String username,
@RequestParam
(
"password"
) String password) {
return
userService.getUser(username,password);
}
// @RequestMapping("/testJedisCluster")
// public User testJedisCluster(@RequestParam("username") String username){
// String value = myRedisTemplate.get(MyConstants.USER_FORWARD_CACHE_PREFIX, username);
// if(StringUtils.isBlank(value)){
// myRedisTemplate.set(MyConstants.USER_FORWARD_CACHE_PREFIX, username, JSON.toJSONString(getUser()));
// return null;
// }
// return JSON.parseObject(value, User.class);
// }
}
|
說明:
以上這些就是最經常使用的幾個註解了。
須要注意的是:
若是ApiImplicitParam中的phone的paramType是query的話,是沒法注入到rest路徑中的,並且若是是path的話,是不須要配置ApiImplicitParam的,即便配置了,其中的value="手機號"也不會在swagger-ui展現出來。
具體其餘的註解,查看:
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
測試:
啓動服務,瀏覽器輸入"http://localhost:8080/swagger-ui.html"
最上邊一個紅框:@Api
GET紅框:method=RequestMethod.GET
右邊紅框:@ApiOperation
parameter紅框:@ApiImplicitParams系列註解
response messages紅框:@ApiResponses系列註解
輸入參數後,點擊"try it out!",查看響應內容:
轉載----https://www.cnblogs.com/gavin-liu/p/6491545.html