全国咨询热线123456789
wwwwxxxx
发布时间:2022-10-16 08:11浏览次数:
  • wwwwxxxx
  • 本号主要用于分享企业中常用的技术,更加侧重于实用,欢迎关注,方便浏览更多实用的历史文章。

    如果你不会Swagger或者掌握的不全面,那么这是一份详尽的文章,让你一次恶补全部知识,请认真学习一下,让大家能学到具体实际有用的知识是我的责任。

    前后端分离,前端和后端API沟通目前企业中几乎都是使用Swagger2。本文主要讲解一下几个案例,基本上工作中都会用到:


    1. Swagger2基本的集成
    2. 对Swagger2配置安全访问
    3. 聚合多个项目

    1. 添加入Swagger2的依赖

    <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version></dependency>

    2. Swagger2Config


    @Configuration@EnableSwagger2public class Swagger2Config { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .groupName("demo") .apiInfo(apiInfo()) .select() // 设置basePackage会将包下的所有类的所有方法作为Api// .apis(RequestHandlerSelectors.basePackage("com.example.demo2.controller")) // 只有标记@ApiOperation才会暴露出给swagger .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.regex("/api/.*")) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API接口文档") .description("RESTful风格接口") .termsOfServiceUrl("https://blog.csdn.net/vbirdbest") // 服务条款网址 .version("1.0") .contact(new Contact("mengday", "httphttp://wwww.xxx.com", "xxx@gmail.com")) .build(); }}

    3. UserController

    @RestController@RequestMapping("/api/v1/users")@Api(value = "User API接口", tags = "user", description = "User API接口")public class UserController { @ApiOperation(value = "用户登录", notes = "用户登录接口") @ApiResponses({ @ApiResponse(code = 0, message = "success"), @ApiResponse(code = 10001, message = "用户名错误", response = IllegalArgumentException.class), @ApiResponse(code = 10002, message = "密码错误") }) @PostMapping(value = "/login") public String login(@ApiParam(name = "username", value = "用户名", required = true) @RequestParam String username, @ApiParam(name = "password", value = "密码", required = true) @RequestParam String password){ return "{'username':'" + username + "', 'password':'" + password + "'}"; } @ApiOperation(value = "修改用户信息", notes = "修改用户信息") @ApiImplicitParams({ @ApiImplicitParam(dataTypeClass = String.class, paramType = "header", name = "phone", required = true, value = "手机号"), @ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "nickname", required = true, value = "nickname", defaultValue = "双击666"), @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "platform", required = true, value = "平台", defaultValue = "PC"), @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "password", required = true, value = "密码") }) @PutMapping(value = "/{platform}/regist") public String regist(@RequestHeader String phone, @RequestParam String nickname, @PathVariable String platform, @RequestBody String password){ return "{'username':'" + phone + "', 'nickname':'" + nickname + "', 'platform': '" + platform + "', 'password':'"+password+"'}"; } @ApiOperation(value = "用户列表", notes = "查询用户列表") @GetMapping(value = "/list") public String getUserList(PagerIDto pager){ return "[{'id': "+pager.getPage()+", 'username': 'zhangsan"+pager.getSize()+"'}]"; } @ApiOperation(value = "删除用户", notes = "删除用户") @DeleteMapping("/{id}") public String removeUser(@PathVariable Long id){ return "success"; } @ApiIgnore @RequestMapping("/ignoreApi") public String ignoreApi(){ return "docs"; }}

  • wwwwxxxx
  • -->