zoukankan      html  css  js  c++  java
  • SpringBoot集成Swagger2

      技术交流群:233513714

    一、创建maven工程引入依赖

    <dependencies>
            <!----------swagge依赖----------->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.7.0</version>
            </dependency>
    
            <!----------jackson依赖----------->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.7</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jsckson-annotions</artifactId>
                <version>2.9.7</version> 
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.9.7</version>
            </dependency>
    </dependencies>

    二、Swagger2配置

    @Configuration  //SpringBoot配置注解
    @EnableSwagger2 //启用Swagger2功能注解
    public class SwaggerConfig {
        @Bean
        public Docket createRestfulApi() {//api文档实例
            return new Docket(DocumentationType.SWAGGER_2)//文档类型:DocumentationType.SWAGGER_2
                    .apiInfo(apiInfo())//api信息
                    .select()//构建api选择器
                    .apis(RequestHandlerSelectors.basePackage("com.datad.dream.controller"))//api选择器选择api的包
                    .paths(PathSelectors.any())//api选择器选择包路径下任何api显示在文档中
                    .build();//创建文档
        }
    
        private ApiInfo apiInfo() {//接口的相关信息
            return new ApiInfoBuilder()
                    .title("SpringBoot使用Swagger2构建RESTful接口")
                    .description("Swagger接口文档路径为:http://localhost:8080/swagger-ui.html")
                    .termsOfServiceUrl("http://localhost:8080/swagger-ui.html") 
    .contact(
    "sunf")
    .version(
    "1.0")
    .license(
    "http://springfox.github.io/springfox/docs/current/")
    .licenseUrl(
    "http://springfox.github.io/springfox/docs/current/")
    .build();
    }
    }

    三、Swagger2注解使@RestController //接口注解@Api(value="用户接口",tags={"catTest"}) //接口简要标注,对中文的支持不太好

    @RequestMapping(value = "/swagger")  //接口基本路径
    public class AnimalController {
        //接口需要的参数,可以有多个,这里只写了一个,它的paramType还有path、query、body、form几种,
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "header", name = "Token", value = "token", dataType = "String", required = true,defaultValue = "123")})
        //接口功能描述
        @ApiOperation(value = "获取一只猫")
        //接口响应信息,这里定义了一个401,当出现401,接口返回的是自定的错误AnimalError的实例。当然可以定义多个。
        @ApiResponses(value = { @ApiResponse(code = 401, message = "请求未通过认证.", response = AnimalError.class) })
        @RequestMapping(value="/onecat", method = RequestMethod.GET)
        public Cat oneCat(){
          Cat cat = new Cat();
         cat.setAge("1");
         cat.setBreed("波斯猫");
         cat.setName("Tom");
            return cat;
        }
    }
    
    @Getter
    @Setter
    @ToString
    @ApiModel(description = "猫咪实体类")
    public class Cat{
      @ApiModeProperty(value = "年龄")
      private String age;
       @ApiModeProperty(value = "品种")
      private String breed;
       @ApiModeProperty(value = "名字")
      private String name;
    }

    四、查看接口信息

    1、在浏览器输入网址http://localhost:8080/swagger-ui.html

    2、展开接口列表

     3、进入接口

     4、调用成功后返回

  • 相关阅读:
    Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor
    Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security
    IdentityServer4 And AspNetCore.Identity Get AccessToken 问题
    Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute
    Java Spring Boot VS .NetCore (七) 配置文件
    Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml
    Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
    Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
    Java Spring Boot VS .NetCore (三)Ioc容器处理
    Java Spring Boot VS .NetCore (二)实现一个过滤器Filter
  • 原文地址:https://www.cnblogs.com/cnndevelop/p/9871531.html
Copyright © 2011-2022 走看看