SpringBoot解决方案之 Swagger生成接口文档
Swagger 是一个用于生成、描述和调用 RESTful 接口的 Web 服务。通俗的来讲,Swagger 就是将项目中所有(想要暴露的)接口展现在页面上,并且可以进行接口调用和测试的服务
实现案例之Swagger2
POM
1 2 3 4 5 6 7 8 9 10 11 12
| <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>
|
Swagger Config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| @EnableSwagger2 @Configuration public class Swagger2Config {
@Bean public Docket adminApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("test GroupName") .apiInfo(adminApiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(Predicates.and(PathSelectors.regex("/admin/.*"))).build(); }
private ApiInfo adminApiInfo(){ return new ApiInfoBuilder() .title("test Title") .description("test description") .version("1.0") .contact(new Contact("c-z-k","http://blog.czk.pub/","940687861@qq.com")) .build(); } @Bean public Docket webApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("webApi") .apiInfo(webApiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(Predicates.and(PathSelectors.regex("/api/.*"))).build(); }
private ApiInfo webApiInfo(){ return new ApiInfoBuilder() .title("API") .description("test description") .version("1.0") .contact(new Contact("c-z-k","http://blog.czk.pub/","940687861@qq.com")) .build(); } @Bean public Docket apiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("api") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(Predicates.and(PathSelectors.regex("/api/.*"))).build(); }
private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("api") .description("test description") .version("1.0") .contact(new Contact("c-z-k","http://blog.czk.pub/","940687861@qq.com")) .build(); } }
|
controller接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
@Api(tags = "会员信息接口")
@RestController @RequestMapping("/api/core/userInfo") public class UserInfoController {
@Resource private UserInfoService userInfoService; @Resource private RedisTemplate redisTemplate;
@ApiOperation("会员注册") @PostMapping("/register") public R register(@RequestBody RegisterVO registerVO){ String mobile = registerVO.getMobile(); String password = registerVO.getPassword(); String code = registerVO.getCode();
Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR); Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR); Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR); Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR); String codeGen = (String) redisTemplate.opsForValue().get("jkr:sms:code:" + mobile); Assert.equals(code,codeGen,ResponseEnum.CODE_ERROR);
userInfoService.register(registerVO);
return R.ok().message("注册成功"); }
@ApiOperation("会员登录") @PostMapping("/login") public R login(@RequestBody LoginVO loginVO, HttpServletRequest request){ String mobile = loginVO.getMobile(); String password = loginVO.getPassword(); Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR); Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
String ip = request.getRemoteAddr(); UserInfoVO userInfoVO = userInfoService.login(loginVO, ip);
return R.ok().data("userInfo",userInfoVO); }
@ApiOperation("校验令牌") @GetMapping("/checkToken") public R checkToken(HttpServletRequest request){ String token = request.getHeader("token"); boolean checkResult = JwtUtils.checkToken(token); if(checkResult){ return R.ok(); }else{ return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR); } }
@ApiOperation("校验手机号是否注册") @GetMapping("/checkMobile/{mobile}") public boolean checkMobile(@PathVariable String mobile){ return userInfoService.checkMobile(mobile); } @ApiOperation("获取个人空间用户信息") @GetMapping("/auth/getIndexUserInfo") public R getIndexUserInfo(HttpServletRequest request) { return R.ok().data("userIndexVO", userIndexVO); } }
|