SpringBoot_5_integrate_Swagger

hayeka發表於2020-03-08
        <!-- 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>
package cn.boxku.config;

import cn.boxku.controller.HelloController;
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 org.springframework.web.bind.annotation.GetMapping;
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;

/**
 * ————————————————————————————————————————————————
 * CREATED BY since ON 2020/3/8 14:09
 * ————————————————————————————————————————————————
 */
@Configuration
@EnableSwagger2 //開啟swagger
public class SwaggerConfig {

    @Bean
    public Docket docket(Environment environment)
    {
        //設定顯示的Swagger環境
        Profiles profiles = Profiles.of("dev","test");
        //獲取專案環境
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("狂神")//團隊
                .enable(flag)//是否啟動swagger
                .select()
                //RequestHandlerSelectors配置要掃描介面的方式
                //basepackage掃描的包
                //any()
                //none()
                //withClassAnnotation掃描類上的註解,引數是一個註解的反射物件
                //withMethodAnnotation掃描 方法上的註解
                .apis(RequestHandlerSelectors.basePackage("cn.boxku.controller"))
                //paths()過濾什麼路徑
                .paths(PathSelectors.ant("/kuang/**"))//開放kuang下面的,其他的沒有
                .build();
    }

    @Bean
    public Docket docket2(Environment environment)
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("docket2");
    }

    @Bean
    public Docket docket3(Environment environment)
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("docket3");
    }

    //配置swagger文件資訊
    private ApiInfo apiInfo()
    {
        //作者資訊
        Contact contact = new Contact("hayeka", "https://www.boxku.cn", "690690661@qq.com");
        return new ApiInfo(
                "hayeka的api文件",
                "盒子世界,世界盒子",
                "v1.0",
                "https://www.boxku.cn",
                 contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList());
    }

}
package cn.boxku.controller;

import cn.boxku.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * ————————————————————————————————————————————————
 * CREATED BY hayeka ON 2020/3/8 14:26
 * ————————————————————————————————————————————————
 */
@RestController
public class HelloController {

    //只要返回中存在實體類,就有掃描到
    @RequestMapping(value = "/kuang/hello",method = RequestMethod.GET)
    public String index()
    {
        return "hello";
    }


    @ApiOperation("我是你爹")
    @RequestMapping(value = "/kuang/hello1",method = RequestMethod.POST)
    public String hello1()
    {
        return "hello1";
    }

    @RequestMapping(value = "/kuang/hello2",method = RequestMethod.GET)
    public String hello2(String name)
    {
        return "hello2"+name;
    }
}
package cn.boxku.pojo;

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

/**
 * ————————————————————————————————————————————————
 * CREATED BY hayeka ON 2020/3/8 15:50
 * ————————————————————————————————————————————————
 */
@ApiModel("使用者實體類")
public class User {

    @ApiModelProperty("使用者名稱")
    public String name;
    @ApiModelProperty("密碼")
    public String password;
}

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8081

application-test.yml

server:
  port: 8082

application-pro.yml

server:
  port: 8083
本作品採用《CC 協議》,轉載必須註明作者和本文連結

一個從事軟體開發職業的經濟愛好者