Swagger

学习视频:https://www.bilibili.com/video/BV1Y441197Lw

简介

官网:https://swagger.io/
springboot集成swagger
1、新建一个springboot项目,选择web
在这里插入图片描述
2、导入相关依赖

<!-- springfox-boot-starter包含 springfox-swagger-ui和springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

3、新建pojo包,JingDian类
做接口测试用

package com.swagger.pojo;
//@ApiModel("景点实体类")   //给实体类加一个注释,方便理解
public class JingDian {
    //@ApiModelProperty("景点名称")  //给实体类的字段添加注释
    public String jdname;
    public String jdaddress;
}

4、新建controller包,SwaggerController类
swagger配置扫描接口

package com.swagger.controller;

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

//@Api(tags = "Swagger控制类")
@RestController
public class SwaggerController {
	/**
     * 第一个swagger工程
     */
    @RequestMapping(value = "/swagger")
    public String changan(){
        return "swagger";
    }

    //只要接口中返回值存在实体类,它就会被扫描到swagger中
    @PostMapping(value = "/jingdian")
    public JingDian jingDian(){
        return new JingDian();
    }

    //Operation接口,不放在类上的,放在方法上
    @ApiOperation("Swagger控制类")
    @GetMapping("/jingdian")
    public String jingdian(@ApiParam(value = "景点名称" ,required = true) String jdname) {
        return "石室" + jdname;
    }

    @ApiOperation("post测试类")
    @PostMapping("/postjd")
    public JingDian postjd(@ApiParam(value = "景点名称" ,required = true)JingDian jdname) {
        //int i=1/0;   测试500错误
        return jdname;
    }
    //访问路径:http://localhost:8080/swagger-ui/index.html
}

4、新建config包,SwaggerConfig类
配置swagger信息、swagger分组

package com.swagger.config;

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.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  //开启swagger2
public class SwaggerConfig {
    //配置了swagger的Docket的bean实例
    //注意environment的包不要导错了---org.springframework.core.env.Environment
    @Bean
     /**
      * 点击Docket 进源码查看 属性---点击DocumentationType查看使用类型
     */
    public Docket docket(Environment environment){
        // 设置要显示swagger的环境
        Profiles of = Profiles.of("dev", "test");
        // 判断当前是否处于该环境
        // 通过 enable() 接收此参数判断是否要显示
        boolean flag = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //这个到企业会用到
                .groupName("changan")
                //是否启用swagger
                .enable(flag)
                .select()
                /**  RequestHandlerSelectors  配置要扫描接口的方式
                 *     其中的方法
                 * basePackage:指定要扫描的包
                 * any():扫描全部
                 * none():不扫描
                 * withClassAnnotation:扫描类上的注解,参数是一个直接的反射
                 * withMethodAnnotation:扫描方法上的注解
                 */
                .apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))
                //扫描swagger包下的所有类
                //paths():过滤....路径
                //.paths(PathSelectors.ant("/swagger/**"))
                .build();
    }
	/**
     * 分组
     * 做swagger右上角的Select a definition
     */
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("guli");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("gucheng");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("gugong");
    }

    //配置swagger信息 --apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("fslm", "https://blog.csdn.net/weixin_44538225", "123456789@qq.com");
        return new ApiInfo(
                "ChangAn Api 文件",
                "既来之则安之",
                "1.0.0",
                "https://blog.csdn.net/weixin_44538225",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}

5、新建配置:application.dev.properties
做swagger配置扫描信息用

server.port=8080

和application.pro.properties

server.port=8087

在application.properties中

# 激活dev
spring.profiles.active=dev

整体的目录
在这里插入图片描述
启动运行:访问http://localhost:8080/swagger-ui/index.html
可以随便查看
在这里插入图片描述
注意:发布的时候先去关闭swagger

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值