Swagger

优点:可以通过swagger给一些属性增加注释信息,接口可以实时更新,可以在线测试 

boot集成swagger 

依赖 可能有错误

错误:Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException 把boot的版本降到2.5.6就可以了

<!--        boot的版本过高可能会有错误-->
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

application.properties

spring.profiles.active=dev

application-dev.properties

server.port=8081

实体类Users

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

@ApiModel("用户实体类")//文档注释
public class Users {
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    private String password;
}

controller 

import com.example.springbootswagger.pojo.Users;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "hello swagger";
    }
    //只要我们的接口中,返回值中存在实体类,他就会扫描的swagger中
    @PostMapping("/users")
    public Users users() {
        return new Users();
    }
    //Operation接口,不是放在类上的,是方法
    @ApiOperation("hello的控制器")
    @PostMapping("/hello2")
    public String hello2(@ApiParam("用户名") String username){
        return "hello"+username;
    }
}

SwaggerConfig

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 springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
    //如何配置多个分组;就是配置多个Docket实例
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    //配置了Swagger的Docket bean实例
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");
        //获取项目的环境
        //通过  environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                //enable(true)是否启用swagger默认为启用(true) 不启用改为false
                .enable(flag)
                //配置API文档的分组
                .groupName("Li")
                //RequestHandlerSelectors配置扫描的方式
//        basePackage指定要扫描包
                //any()扫描所有
                //none()都不扫描
                //withClassAnnotation(RestController.class)扫描类上的注解 ,参数是一个注解的反射对象前面这种是(只扫描带有RestController的类)
                //withMethodAnnotation(GetMapping.class)扫描方法上的注解(代表只扫描方法上带有GetMapping注解的方法)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.springbootswagger.controller"))
                //过滤
                //any()所有都扫描
                //none() 全都不扫描
                //ant("/user/**")只扫描/user/下的请求
                //regex("")正则表达式
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("小白", "https://mp.csdn.net/mp_blog/manage/article?spm=1000.2115.3001.5448", "1374216232@qq.com");
        return new ApiInfo("A Li 的首页",
                "时间是最好的证明",
                "L1.0",
                "urn:tos",
                contact,
                "Li 2.0",
                "https://mp.csdn.net/mp_blog/manage/article?spm=1000.2115.3001.5448",
                new ArrayList<VendorExtension>());
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值