SpringFox 3尝鲜 集成SpringBoot生成Swagger接口文档

SpringFox简介

SpringFox 是一个开源的API Doc的框架, 它的前身是swagger-springmvc,可以将我们的Controller中的方法以文档的形式展现。 官方定义为: Automated JSON API documentation for API's built with Spring。

SpringFox3快速应用

  1.    maven项目依赖
    
        io.springfox
        springfox-boot-starter
        3.0.0
    
  2. 配置类信息
    package com.kongliand.swagger3.config;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.util.StringUtils;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class SwaggerUiWebMvcConfigurer implements WebMvcConfigurer {
      private final String baseUrl;
    
      public SwaggerUiWebMvcConfigurer(
          @Value("${springfox.documentation.swagger-ui.base-url:}") String baseUrl) {
        this.baseUrl = baseUrl;
      }
    
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
        registry.
            addResourceHandler(baseUrl + "/swagger-ui/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
            .resourceChain(false);
      }
    
      @Override
      public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(baseUrl + "/swagger-ui/")
            .setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
      }
    
      @Override
      public void addCorsMappings(CorsRegistry registry) {
        registry
            .addMapping("/api/pet")
            .allowedOrigins("http://editor.swagger.io");
        registry
            .addMapping("/v2/api-docs.*")
            .allowedOrigins("http://editor.swagger.io");
      }
    }
  3. 用户实体类
    package com.kongliand.swagger3.model;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import javax.validation.constraints.*;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("用户基本信息")
    public class User {
    
    
        @ApiModelProperty("编号")
        private Long id;
    
        @ApiModelProperty("姓名")
        private String name;
    
        @ApiModelProperty("年龄")
        private Integer age;
    
        @ApiModelProperty("地址")
        private String address;
    
        @ApiModelProperty("邮箱")
        private String email;
    
    }
  4. 用户接口访问控制层
    package com.kongliand.swagger3.controller;
    
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.kongliand.swagger3.model.User;
    import com.kongliand.swagger3.service.UserSerivce;
    import io.swagger.annotations.*;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import springfox.documentation.annotations.ApiIgnore;
    
    import javax.validation.Valid;
    import javax.validation.constraints.*;
    import java.util.ArrayList;
    import java.util.List;
    
    @Api(tags="用户管理")
    @RestController
    public class UserController {
    
    
        @Autowired
        private UserSerivce userSerivce;
    
        @ApiOperation("添加用户")
        @PostMapping("/save")
        public int create(@RequestBody @Valid User user) {
            return userSerivce.save(user);
        }
    
        @ApiOperation("用户详情")
        @GetMapping("/user/{id}")
        public User findById(@PathVariable Long id) {
            return userSerivce.findById(id);
        }
    
        @ApiOperation("用户列表")
        @GetMapping("/findAll")
        public Page list(@ApiParam("查看第几页") @RequestParam int pageIndex,
                               @ApiParam("每页多少条") @RequestParam int pageSize) {
            return userSerivce.findByPage(pageIndex,pageSize);
        }
    
        @ApiOperation("删除用户")
        @DeleteMapping("/delete/{id}")
        public int  deleteById(@PathVariable Long id) {
            return userSerivce.deleteById(id);
        }
    
    }
    
    
  5. Spring Boot程序启动入口添加 @EnableOpenApi注解
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import springfox.documentation.oas.annotations.EnableOpenApi;
    
    /**
     * @author kevin
     */
    @SpringBootApplication
    @EnableOpenApi
    @MapperScan("com.kongliand.swagger3.mapper")
    public class  Swagger3Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Swagger3Application.class, args);
        }
    
    }
  6. 访问http://localhost:8081/swagger-ui/
    SpringFox 3尝鲜 集成SpringBoot生成Swagger接口文档_第1张图片

你可能感兴趣的:(springboot系列)