|
4 | 4 | “springboot-helloworld-2.2.5.RELEASE.jar中没有主清单属性” |
5 | 5 |
|
6 | 6 |
|
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 | + * 其他注解自查 |
9 | 64 |
|
10 | 65 | ### 引用 |
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中定义的扫描文件等 |
0 commit comments