Skip to content

Commit ee92881

Browse files
committed
docs(swaggger): add note about integrate springboot with swagger to show apis
1 parent aa23e22 commit ee92881

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

java/spring/spring-boot/springboot-note.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,67 @@
44
“springboot-helloworld-2.2.5.RELEASE.jar中没有主清单属性”
55

66

7-
* Mapped Statements collection does not contain value for
8-
多数时mapper文件的问题,文件名/方法名/在properties中定义的扫描文件等
7+
8+
9+
### swagger
10+
* 配置依赖swagger2
11+
```xml
12+
<dependency>
13+
<groupId>io.springfox</groupId>
14+
<artifictId>springfox-swagger2</artifictId>
15+
<dependency>
16+
<dependency>
17+
<groupId>io.springfox</groupId>
18+
<artifictId>springfox-swagger-ui</artifictId>
19+
<dependency>
20+
```
21+
* 配置Swagger2所需的Docket Bean
22+
```java
23+
@Configuration
24+
@EnableSwagger2
25+
public class Swagger2Config {
26+
27+
private ApiInfo apiInfo() {
28+
return new ApiInfoBuilder()
29+
.title("SpringBoot使用Swagger2构建api文档")
30+
.description("文档的详细描述")
31+
.version("1.0")
32+
.build();
33+
}
34+
35+
@Bean
36+
public Docket createRestApi() {
37+
return new Docket(DocumentationType.SWAGGER_2)
38+
.apiInfo(apiInfo())
39+
.select()
40+
.apis(RequestHandlerSelectors.basePackage("com.zhonghuasheng.swagger.controller"))
41+
.paths(PathSelectors.any())
42+
.build();
43+
}
44+
}
45+
```
46+
* 使用swagger注解在controller上
47+
```java
48+
@ApiOperation(value = "获取用户", notes = "根据url中的id获取用户信息")
49+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
50+
public User get(@PathVariable Long id) {
51+
return null;
52+
}
53+
```
54+
* 配置好swagger后访问http://localhost:8080/swagger-ui.html
55+
* Swagger提供的注解
56+
* @Api:修饰整个类,用于描述Controller类
57+
* @ApiOperation:描述类的方法或者说是一个接口
58+
* @ApiParam: 单个参数描述
59+
* @ApiModel: 用对象来接收参数
60+
* @ApiResponse:HTTP响应的一个描述
61+
* @ApiIgnore: swagger会忽略这个方法
62+
* @ApiError: 发生错误返回的信息
63+
* 其他注解自查
964

1065
### 引用
11-
* 理解spring-boot-starter-parent的作用 https://www.jianshu.com/p/628acadbe3d8
66+
* 理解spring-boot-starter-parent的作用 https://www.jianshu.com/p/628acadbe3d8
67+
68+
### 常见问题
69+
* Mapped Statements collection does not contain value for
70+
多数时mapper文件的问题,文件名/方法名/在properties中定义的扫描文件等

java/spring/spring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* [SpringBoot Shiro]
6161
* [SpringBoot Session]()
6262
* [SpringBoot Log4j2](https://github.com/zhonghuasheng/JAVA/tree/master/springboot)
63+
* [SpringBoot Swagger](spring-boot/springboot-note.md#swagger)
6364

6465
> SpringBoot百问
6566

0 commit comments

Comments
 (0)