Springboot集成Swagger操作步骤
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。
作用:
- 接口的文档在线自动生成。
- 功能测试。
配置
第一步:配置pom.xml
<dependencies> ... <!-- swagger www.1b23.com --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version> </dependency> <!-- swagger-ui www.1b23.com --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version> </dependency> </dependencies>
第二步:IDEA执行Reimport All Maven Projects
第三步:使用注解来进行启动swagger
package com.template.swagger; 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; /** * www.1b23.com */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("org.fh.controller")) .paths(PathSelectors.any()) .build(); // return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("Spring Boot 使用 Swagger2 构建RESTful API") //创建人 .contact(new Contact("Bryan", "http://blog.bianxh.top/", "")) //版本号 .version("1.0") //描述 .description("API 描述") .build(); } }
第四步:配置Controller
package com.template.controller; /** * www.1b23.com */ @Api(description = "用户操作接口") @Controller("user") @RequestMapping("/user") public class UserController extends BaseController { //... @ApiOperation(value = "获取otp", notes="通过手机号获取OTP验证码") @ApiImplicitParam(name = "telephone", value = "电话号码", paramType = "query", required = true, dataType = "Integer") @RequestMapping(value = "getotp", method=RequestMethod.GET) @ResponseBody public CommonReturnType getOtp(@RequestParam(name = "telephone") String telphone) { //需要按照一定的规则生成OTP验证码 Random random = new Random(); int randomInt = random.nextInt(99999); randomInt += 10000; String otpCode = String.valueOf(randomInt); //将OTP验证码同对应用户的手机号关,使用httpsession的方式绑定他的手机号与OTPCode httpServletRequest.getSession().setAttribute(telphone,otpCode); //将OTP验证码通过短信通道发送给用户,省略 System.out.println("telphone = " + telphone + "& otpCode = " + otpCode); OtpVo otpVo = new OtpVo(); otpVo.setTelephone(telphone); otpVo.setOtpCode(otpCode); return CommonReturnType.create(otpVo); } //... }
相关推荐
莫问前程 2020-06-14
SAMXIE 2020-04-17
pontuss 2020-04-09
smalllove 2020-03-27
meleto 2020-03-05
SAMXIE 2020-11-04
XuDanT 2020-09-16
permanent00 2020-09-15
哈嘿Blog 2020-09-08
Qizonghui 2020-08-02
莫问前程 2020-08-02
SAMXIE 2020-07-26
XuDanT 2020-07-24
莫问前程 2020-07-18
Qizonghui 2020-07-18
coolhty 2020-07-05
Qizonghui 2020-06-28
Qizonghui 2020-06-25
SAMXIE 2020-06-14