SpringBoot整合Swagger2
1.pom里面引入swagger2的依赖
<!-- swagger2 配置 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
2.添加Swagger2配置类
package com.imooc; import java.util.ArrayList; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.Parameter; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2 { /** * @Description:swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等 */ @Bean public Docket createRestApi() { // 为swagger添加header参数可供输入 ParameterBuilder userTokenHeader = new ParameterBuilder(); ParameterBuilder userIdHeader = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); userTokenHeader.name("headerUserToken").description("userToken") .modelRef(new ModelRef("string")).parameterType("header") .required(false).build(); userIdHeader.name("headerUserId").description("userId") .modelRef(new ModelRef("string")).parameterType("header") .required(false).build(); pars.add(userTokenHeader.build()); pars.add(userIdHeader.build()); return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.imooc.controller")) .paths(PathSelectors.any()).build() .globalOperationParameters(pars); } /** * @Description: 构建 api文档的信息 */ private ApiInfo apiInfo() { return new ApiInfoBuilder() // 设置页面标题 .title("使用swagger2构建短视频后端api接口文档") // 设置联系人 .contact(new Contact("southbean", "http://www.southbean.com", "ss@126.com")) // 描述 .description("欢迎访问短视频接口文档,这里是描述信息") // 定义版本号 .version("1.0").build(); } }
3.在controller中引入swagger2的api
@RestController @Api(value="用户注册登录的接口", tags= {"注册和登录的controller"}) public class RegistLoginController extends BasicController{ @Autowired private UserService userService; @ApiOperation(value="用户注册", notes="用户注册的接口") @PostMapping("/regist") public IMoocJSONResult regist(@RequestBody Users user) throws Exception { // 1. 判断用户名和密码必须不为空 if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) { return IMoocJSONResult.errorMsg("用户名和密码不能为空"); } // 2. 判断用户名是否存在 boolean usernameIsExist = userService.queryUsernameIsExist(user.getUsername()); // 3. 保存用户,注册信息 if (!usernameIsExist) { user.setNickname(user.getUsername()); user.setPassword(MD5Utils.getMD5Str(user.getPassword())); user.setFansCounts(0); user.setReceiveLikeCounts(0); user.setFollowCounts(0); userService.saveUser(user); } else { return IMoocJSONResult.errorMsg("用户名已经存在,请换一个再试"); } user.setPassword("");
<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>