springboot 2.0集成swagger

①首先创建springboot项目,然后引入swagger的jar包。

注意,如果引入的是swagger2.0版本的,那么只需要引入两个jar包

       
            io.springfox
            springfox-swagger2
            2.0.0
        

        
        
            io.springfox
            springfox-swagger-ui
            2.0.0
        

 

然后创建一个配置类,如下

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

接着就可以运行主函数,然后浏览器访问:

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

如果你是3.0版本的swagger,那么就需要引入如下jar包:

        
        
            io.springfox
            springfox-swagger2
            3.0.0
        

        
        
            io.springfox
            springfox-swagger-ui
            3.0.0
        

        
            io.springfox
            springfox-boot-starter
            3.0.0
        

,配置类可以继续使用上方的配置类,接着就启动项目,运行如下地址:

http://localhost:8080/swagger-ui/index.html

②配置swagger信息

原来的配置信息

springboot 2.0集成swagger_第1张图片

修改配置信息:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
public class SwaggerConfig {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }


    //配置swagger文档
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("zz","http://www.qq.com","[email protected]");
        return new ApiInfo("主题",
                "描述",
                "2.0",
                "urn:tos",
                contact,
                "Apache--2.0",
                "http://www.baidu.com",
                new ArrayList<>());
    }
}

修改后的信息:

springboot 2.0集成swagger_第2张图片springboot 2.0集成swagger_第3张图片

③设置扫描路径:

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 org.springframework.web.bind.annotation.RestController;
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
public class SwaggerConfig {

    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");
        boolean b = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable:是否开启swagger,默认true
                .enable(b)
                .select()
                //RequestHandlerSelectors,配置要扫描的接口的方式
                //basePackage:指定要扫描的包(常用)
                .apis(RequestHandlerSelectors.basePackage("com.zz.demo.controller"))
                //any():扫描全部
                //.apis(RequestHandlerSelectors.any())
                //none():不扫描
                //.apis(RequestHandlerSelectors.none())
                //withClassAnnotation():扫描类上的注解,参数是一个注解反射对象
                //.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                //withMethodAnnotation():扫描方法上的注解
                //.apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                //paths:过滤路径
                .paths(PathSelectors.ant("/hello/**"))
                .build();
    }


    //配置swagger文档
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("zz","http://www.qq.com","[email protected]");
        return new ApiInfo("主题",
                "描述",
                "2.0",
                "urn:tos",
                contact,
                "Apache--2.0",
                "http://www.baidu.com",
                new ArrayList<>());
    }
}

④设置分组:

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 org.springframework.web.bind.annotation.RestController;
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
public class SwaggerConfig {

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

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

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

    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");
        boolean b = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable:是否开启swagger,默认true
                .enable(!b)
                .select()
                //RequestHandlerSelectors,配置要扫描的接口的方式
                //basePackage:指定要扫描的包(常用)
                .apis(RequestHandlerSelectors.basePackage("com.zz.demo.controller"))
                //any():扫描全部
                //.apis(RequestHandlerSelectors.any())
                //none():不扫描
                //.apis(RequestHandlerSelectors.none())
                //withClassAnnotation():扫描类上的注解,参数是一个注解反射对象
                //.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                //withMethodAnnotation():扫描方法上的注解
                //.apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                //paths:过滤路径
                .paths(PathSelectors.ant("/hello/**"))
                .build();
    }


    //配置swagger文档
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("zz","http://www.qq.com","[email protected]");
        return new ApiInfo("主题",
                "描述",
                "2.0",
                "urn:tos",
                contact,
                "Apache--2.0",
                "http://www.baidu.com",
                new ArrayList<>());
    }
}

springboot 2.0集成swagger_第4张图片

⑤API注释:

  1)写在pojo类上:

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

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("姓名")
    private String name;
    @ApiModelProperty("年龄")
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

效果:

springboot 2.0集成swagger_第5张图片

主要作用于Model下。

     2)写在controller类上:

import com.zz.demo.pojo.User;
import io.swagger.annotations.Api;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@ApiOperation("Hello控制类")
@RestController
public class HelloWorld {

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @ApiOperation("返回用户类")
    @PostMapping("/user")
    public User user(){
        return new User();
    }
}

效果:

springboot 2.0集成swagger_第6张图片

此外,可以使用效果更好看的knife4j。

参考官网:https://doc.xiaominfo.com/knife4j/documentation/get_start.html

用法差不多

你可能感兴趣的:(java)