1.添加 Pom 文件
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
2. 添加配置类 SwaggerConfig.java
package com.vipsoft.web.boot.config; import springfox.documentation.service.Contact; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage("com.vipsoft.model.user.controller")) .paths(PathSelectors.any()) .build().apiInfo(apiInfo()); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SpringBoot整合Swagger") .description("SpringBoot整合Swagger,API接口描述......") .version("9.0") .contact(new Contact("VipSoft","www.vipsoft.com.cn","47262947@qq.com")) .license("The Apache License") .licenseUrl("http://www.baidu.com") .build(); } }
3. 添加接口说明
需要暴露出去的实体
/** * 用户实体 */ @ApiModel public class Users { @ApiModelProperty(value = "用户id") private String Id; @ApiModelProperty(value = "用户名") private String userName; @ApiModelProperty(value = "密码") private String password; @ApiModelProperty(value = "真实姓名") private String realName; private Date createTime; 。。。。。 }
Controller
@Api(description = "用户操作接口") @RestController @RequestMapping("/user") public class UserController { Logger logger = LoggerFactory.getLogger(UserController.class); Logger loggerApiHis = LoggerFactory.getLogger("apiHis"); //apiHis 对应 logback.xml中的配置 /** * Mybatis 获取 List 列表 * * @param request * @return */ @ApiOperation(value = "获取用户信息", notes = "这边可以不写") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主健", defaultValue = "深圳", required = true) }) @GetMapping(value = "/get/{id}") public Users get(HttpServletRequest request, @PathVariable("id") String id) { return userService.get(id); }
启动类上加@EnableSwagger2 否则会报 Swagger2 Unable to infer base url. This is common (降低版本好像也可以没试过)
输入访问地址:http://localhost:8080/swagger-ui.htm
原文:https://www.cnblogs.com/vipsoft/p/13170766.html