使用swagger测试接口

swagger:自动扫描 controller 包下的请求,生成接口文档,并提供测试功能。
1.引入依赖

       <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        
            io.springfox
            springfox-swagger2
            2.9.2
        

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
  1. 在 config 包引入 swagger 自定义配置类
package com.zhiyou100.zymusic.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;

/**
 * @author teacher
 * @date 2019/9/25
 */
@Configuration
@EnableSwagger2
public class MySwaggerConfiguration {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //标题
                .title("Spring Boot 中使用 Swagger2 构建 RESTful APIs")
                //简介
                .description("hello swagger")
                //服务条款
                .termsOfServiceUrl("1. xxx\n2. xxx\n3. xxx")
                //作者个人信息
                .contact(new Contact("admin", "http://www.zhiyou100.com", "[email protected]"))
                //版本
                .version("1.0")
                .build();
    }
}

  1. 启动项目后,使用 http://localhost:8080/swagger-ui.html

  2. 选择需要测试的接口:Try it out -> 填写参数 -> Execute -> 查看响应

你可能感兴趣的:(spingboot,插件)