swagger的学习

1.创建一个Springboot的web项目

2.引入maven依赖

     <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

3.编写hello world

  项目结构

  

4.访问 http://127.0.0.1:8081/swagger-ui.html  页面

 5.配置Swagger

  1) config 包下的SwaggerConfig文件

package com.lh.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @program: springfox-swagger
 * @description: Swagger 的配置文件
 * @author: li hui
 * @create: 2020-12-23 18:30
 */
@Configuration
@EnableSwagger2    //开启Swagger2
public class SwaggerConfig {
  
//多个 Docket 是我们的 swagger-ui.html 网页中的组
@Bean
public Docket docket1(){ return new Docket(DocumentationType.SWAGGER_2)
          //组的名字叫A .groupName(
"A"); } // 配置Swagger的 docket 的bean实例 @Bean public Docket docket(Environment environment){ /* 我只希望我的Swagger 在生产环境中可以使用,再发布时不可使用 Environment Profiles */ //设定要显示Swagger 环境 Profiles profiles = Profiles.of("dev","test"); //通过 environment.acceptsProfiles 判断是否处在自己的设定环境当中 boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2)
          //组的名字叫 回味 .groupName(
"回味") .enable(flag)// enable 是否开启Swagger 如果false 则Swagger访问在浏览器中访问 .apiInfo(apiInfo()) // 在swagger-ui.html 中 是 swagger 的信息 .select()    // 配置扫描接口 /* RequestHandlerSelectors 配置要扫描接口的方式 basePackage 指定要扫描的包 any 全部扫描 none 不扫描 withMethodAnnotation 扫描方法上的注解 参数是注解的反射的一个对象 withClassAnnotation 扫描类上的注解 */ .apis(RequestHandlerSelectors.basePackage("com.lh.swagger.controller")) // 过滤什么路径 //.paths(PathSelectors.ant("com/lh/**")) .build(); } //配置 Swagger 的信息 = apiInfo private ApiInfo apiInfo(){ //作者信息 Contact contact = new Contact("李辉", "https://www.cnblogs.com/lihui123/", "li_hui123@qq.com"); return new ApiInfo( "回味文档" , "正在学习。。。。" , "1.0" , "https://www.cnblogs.com/lihui123/" , contact , "Apache 2.0" , "http://www.apache.org/licenses/LICENSE-2.0" , new ArrayList()); } }

  2) 文件application.properties

server.port=8080
spring.profiles.active=dev    # 开启生产者模式

  3)文件application-dev.properties

server.port=8081

  4)文件 application-pro.properties

server.port=8081

  5)bean 包下的 user 

package com.lh.swagger.bean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @program: springfox-swagger
 * @description:
 * @author: li hui
 * @create: 2020-12-23 20:35
 */
//@Api(注释)
@ApiModel("用户实体类")   // 给实体类 注解
public class User {
    @ApiModelProperty("用户名")  // 给实体类的字段 注解
    private String username;
    @ApiModelProperty("用户密码")  // 给实体类的字段 注解
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

  6) controller包下的 HelloController

package com.lh.swagger.controller;

import com.lh.swagger.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: springfox-swagger
 * @description:
 * @author: li hui
 * @create: 2020-12-23 18:24
 */
@RestController
public class HelloController {

    @GetMapping("/hello")
    @ApiOperation("测试 hello")     // 给接口的方法 注解
    public String hello(){
        return "hello Swagger";
    }

    @PostMapping("/aa")
    @ApiOperation("测试 实体类")   // 给接口的方法 注解
    public User users(){
        return new User();
    }

    @PostMapping("/bb")
    @ApiOperation("测试 参数")   // 给接口的方法 注解
    public User users(@RequestParam @ApiParam("用户名") String username,@RequestParam @ApiParam("用户密码")  String password){
        System.out.println(username+"   "+password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return user;
    }
}

 6. 使用 swagger-ui.html 进行接口的测试

 

原文地址:https://www.cnblogs.com/lihui123/p/14181497.html