还在苦苦使用postman?——使用springboot整合swagger,在线接口测试文档

0.首先看一眼效果,可以直接传参进行测试哦!

在这里插入图片描述
在这里插入图片描述

1.添加依赖到pom.xml中

<!--swagger-->
 <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>

2.添加配置类SwaggerConfig.java

注意一点:controller中的映射推荐使用@GetMapping这种形式,如果使用@RequestMapping的形式,会让一个接口产生所有请求类型的方法(post,get,put,,,,)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

/**
 * @author lk
 * @date 2020/7/18 19:23
 * @description: swagger接口文档配置
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    
    @Bean
    public Docket docket(){
    
    
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                //注意下面的com.zjgsu.xxxy.controller换成你自己的controller的位置
                .select().apis(RequestHandlerSelectors.basePackage("com.zjgsu.xxxy.controller"))
                .build();
    }
    private ApiInfo apiInfo(){
    
    
        // 作者信息
        Contact contact = new Contact("lk"," "," ");

        return new ApiInfo(
                "lk",
                "文档",
                "v1.0",
                " ",
                contact,
                "Apach 2.0",
                "http://www.apach.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }

}

3.测试

在你的端口号后面添加 /swagger-ui.html 直接访问即可
http://127.0.0.1:8080/swagger-ui.html

猜你喜欢

转载自blog.csdn.net/qq_44625080/article/details/107434046
今日推荐