springboot整合springfox-swagger2

引入pom



        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         


    
            io.springfox
            springfox-swagger2
            2.9.2
    
    
            io.springfox
            springfox-swagger-ui
            2.9.2
    

配置swagger配置类

SwaggerConfig.class

package com.lee.room.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
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;
import java.util.function.Predicate;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

/**
 * @Author li.heng
 * @Date 2019/6/25 17
 * @Description: EnableSwagger2注解 开启Swagger支持
 **/
@EnableSwagger2
@Configuration
@ComponentScan("com.lee.room.controller.*")
public class SwaggerConfig {


    @Bean
    public Docket swaggerCoreConfig() {
        // 构造函数传入初始化规范,这是swagger2规范
        return new Docket(DocumentationType.SWAGGER_2)
                // 添加api详情信息
                .apiInfo(getInfo())
                // 添加默认参数列表
                .globalOperationParameters(new ArrayList<>())
                .select()
                // 添加过滤条件,
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 这里是控制哪些路径的api会被显示出来,比如下方的参数就是除了/user以外的其它路径都会生成api文档
                .paths((String a) ->
                        !a.equals("/user"))
                .build();
    }

    private ApiInfo getInfo() {
        // contact为联系信息对象,在访问接口页面时会呈现,参数一为名称,参数二为联系url,参数三为
        return new ApiInfoBuilder()
                .contact(
                        new Contact("小笼包",
                                "https://github.com/lhdhr5828",
                                "[email protected]"))
                .title("room api")
                .description("房产信息project")
                .termsOfServiceUrl("https://www.baidu.com")
                .extensions(new ArrayList<>(16))
                .license("许可信息")
                .version("1.0").build();
    }
}

新建Controller

IndexController.class

package com.lee.room.controller;


import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author heng.li
 * @Date 2019/4/11 13
 * @Description
 **/
@RestController
public class IndexController {

    /**
     * @return
     */
    @ApiOperation(value = "index接口")
    @GetMapping(value = "/")
    public String index() {
        return "This is room project";
    }

    /**
     * tags 类似于group,下面会有一系列的接口
     * httpMethod是指接口页面上显示的请求方法
     * @param str
     * @return
     */
    @ApiOperation(value = "测试有参数接口", notes = "note", tags = "tags", httpMethod = "POST", protocols = "protocols")
    @PostMapping(value = "/get/str")
    public String getInfo(String str) {
        return "test param";
    }
}

application.properties

server.port=8080

启动项目

访问http://localhost:8080/swagger-ui.html

api文档主页面

点击接口还可以进行请求测试

单个接口

测试结果:

测试结果

你可能感兴趣的:(springboot整合springfox-swagger2)