Spring Cloud 整合 Swagger2 实现在线接口文档 (2)
这是我参与更文挑战的第14天,活动详情查看: 更文挑战
接着上一篇文章继续对业务层module整合Swagger
在业务module添加swagger2 maven 依赖
<dependency><!--添加Swagger依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency><!--添加Swagger-UI依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--添加以下配置,java.lang.NumberFormatException: For input string: ""
解决Swagger2.9.2的NumberFormatException问题-->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
在业务module 添加SwaggerConfig文件
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @ClassName: SwaggerConfig
* @Description: 以yml文件配置方式配置swagger,类配置swagger属性已弃用
* @Author Gxin
* @Date 2021/6/26 16:32
* @Version: 1.0
**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("swagger相关注解所在的包路径"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Swagger API")
.description("接口文档")
.termsOfServiceUrl("团队网站")
.contact(new Contact("作者名","","电子邮箱"))
.version("版本号")
.build();
}
}
以application.yml文件配置swagger网页参数
swagger:
base-package: com.ozx.ozxshopservicemember.controller #swagger相关注解所在的包路径
title: SpringBoot2.x构建集成swagger2项目
description: API文档自动生成
version: 1.1
terms-of-service-url:
contact:
name: Gxin
email: 电子邮箱
url:
添加Controller
package com.ozx.ozxshopservicemember.controller;
import com.alibaba.fastjson.JSONObject;
import com.ozx.ozxshopapimemberdto.input.UserInpDTO;
import com.ozx.ozxshopcommon.basic.BasicResponse;
import com.ozx.ozxshopserviceapimember.service.UserRegisterService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName: UserResigterController
* @Description: 用户注册模块
* @Author Gxin
* @Date 2021/6/26 15:15
* @Version: 1.0
**/
@RestController
@RequestMapping("/mem")
@ApiModel("用户注册模块")
public class UserResigterController {
@Autowired
private UserRegisterService userRegisterService;
@PostMapping("/reg")
@ApiOperation(value = "注册",notes = "注册接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "regCode",paramType = "query",value = "验证码",dataType = "String",example = "12345")
}
)
public BasicResponse<JSONObject> register(UserInpDTO userInpDTO,String regCode){
return userRegisterService.register(userInpDTO, regCode);
}
}