轻松几步上手Swager

引言

Swager其实学习起来并不难,只需要一个小时左右,但是它的作用值得我们一提,
Swager就是可以利用登录/Swager-ui.html页面进行后端的接口测试,其页面功能非常简单

第一步

导入Swagger的pom依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

配置config文件夹,里面创一个Config类为了准备配置Swager配置文件
轻松几步上手Swager_第1张图片
接下来看看类里面咋写的

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.RequestHandler;
import springfox.documentation.builders.PathSelectors;
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;
@Configuration
@EnableSwagger2  //开启swagger2
public class SwaggerConfig {

   @Bean
    public Docket docket(Environment environment) {
      //设置要显示的Swagger环境
      Profiles profiles= Profiles.of("dev","test");   //这里我写了一个生产环境一个测试环境,一般Swagger是用于测试环境来联调接口
      //获取项目的环境
      boolean flag =environment.acceptsProfiles(profiles);
     return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
        .enable(flag)  //是否启动Swagger,false关闭Swagger
        .groupName("李川")    //给自己设置名字
        .select()
        // RequestHandlerSelectors:配置要扫描接口的方式
        //basePackage:指定要扫描的包
        //any():扫描全部
        //none():不扫描
        //withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
        //withMethodAnnotation:扫描方法上的注解
        .apis(RequestHandlerSelectors.basePackage("com.li.swager.controller"))
        //.paths(PathSelectors.ant("/li/**"))  //过滤的路劲
        .build()}
        //配置Swagger信息=apiInfo
    public ApiInfo apiInfo() {
        final Contact contact = new Contact("李川", "https://blog.csdn.net/jiohfgj", "[email protected]");
        return new ApiInfo("测试Swagger的Api文档",       
                "即使",           
                "v1.0",
                "http",
                contact,
                "Apache 2.0",
                "https://blog.csdn.net/jiohfgj",
                new ArrayList()
        );
    }
}

如图:
轻松几步上手Swager_第2张图片

轻松几步上手Swager_第3张图片

后端接口传参测试

import com.li.swager.model.User;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping(value="/hello")
    public String hello(){
        return  "hello";
    }
    //只要我们的接口中返回有实体类就会接受
@PostMapping("/user")
public User user(){
    return  new User();
}
@ApiOperation("Hello控制类")
@GetMapping("/hello2")
public String hello2(@ApiParam("用户名") String username){
    return  "hello"+username;
}
@ApiOperation("Post控制类")
@GetMapping("/postt")
public User postt(@ApiParam("用户") User user){
    return  user;
}

轻松几步上手Swager_第4张图片

如何测试接口

轻松几步上手Swager_第5张图片

轻松几步上手Swager_第6张图片
轻松几步上手Swager_第7张图片

中文注解

@ApiOperation(“Post控制类”) 写在方法上
@ApiModel(“用户实体类”) 写在类上
@ApiModelProperty(“密码”) 写在字段上

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

轻松几步上手Swager_第8张图片

角色的创建

每个角色对应的后端接口在Swager页面的显示不一样,而我们可以利用下面代码进行设置角色
这些放入Swagger配置类里面

@Bean
public Docket docket1(){
    return   new Docket(DocumentationType.SWAGGER_2).groupName("第二个");
}
@Bean
public Docket docket2(){
    return   new Docket(DocumentationType.SWAGGER_2).groupName("第三个");
}
@Bean
public Docket docket3(){
    return   new Docket(DocumentationType.SWAGGER_2).groupName("第四个");
}

你可能感兴趣的:(Swagger)