springboot完美整合swagger

本文介绍了如何在SpringBoot项目中完美整合Swagger2,从引入依赖、配置 Swagger2 类、创建 Controller 层到 YAML 配置,一步步展示了一个简单的 Swagger2 示例。通过配置,实现了接口文档的自动化,方便开发和测试。
摘要由CSDN通过智能技术生成

swagger还是无意间和大学同学在讨论技术的时候听到他讲到的,
我们公司是没有用这个的,所以具有强迫症的我决定琢磨一下!!!话不多说上图开始

首先贴出demo整体结构

图片:
在这里插入图片描述
两个controller是两个测试controller,建立的swagger2和boot启动类一层

项目开始导入所需maven jar包

	<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>

配置Swagger2类

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
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.List;

/**
* @auth liuxiangqian
* @date 2019/8/20 9:51
 */
@Configuration
@EnableSwagger2
/*结合yml配置生产环境屏蔽swagger不然很容易让人利用swagger执行对库操作*/
@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = 			"true")
public class Swagger2 {

@Bean
public Docket createRestApi(){

ParameterBuilder parameterBuilder=new ParameterBuilder();
List<Parameter> list=new ArrayList<>();

parameterBuilder.name("name").description("当前使用者姓名").modelRef(new ModelRef("string"))
        .parameterType("query").required(false).build();

    list.add(parameterBuilder.build());

    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))
            .paths(PathSelectors.any())
            .build().globalOperationParameters(list);
}
@SuppressWarnings("deprecation")
private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
            .title("个人测试")
            .description("个人测试API")
            .termsOfServiceUrl("")
            .contact("测试")
            .version("1.0")
            .build();
}
}

介绍下这些参数的含义,当然你可以点击查看源码就会一目了然

parameterBuilder.name("name").description("当前使用者姓名").modelRef(new 	 ModelRef("string")).parameterType("query").required(false).build();

这段代码是默认代码,会在每个测试接口出现的公共参数。

return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))
            .paths(PathSelectors.any())
            .build().globalOperationParameters(list);
}

那这段代码就是对我们定义的apiInfo方法的调用,controller扫描路径和统一添加header 参数的方法

@SuppressWarnings("deprecation")
private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
            .title("个人测试")
            .description("个人测试API")
            .termsOfServiceUrl("")
            .contact("测试")
            .version("1.0")
            .build();
}

title参数是显示在http://localhost:8080/swagger-ui.html页面的头部,.description会显示在title下方,下面几个参数也是显示作用

创建Controller层

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
* @auth liuxiangqian
* @date 2019/8/20 15:32
*/
@Controller
@RequestMapping("/shop")
@Api(value = "购物模块Controller")
public class ShopController {

@RequestMapping("/goShopping")
@ResponseBody
@ApiOperation(value = "商品模块接口",notes = "去购物入口接口",httpMethod = "POST")
@ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "商品编码",dataType = "int",required = true,paramType = "form"),
        @ApiImplicitParam(name = "name",value = "商品名字",dataType = "string",required = true,paramType = "form")
})
public String goShopping(int id,String name){
    System.out.println("商品ID:"+id+"名字:"+name+"已出库");
    return "成功购买";
}
}

那这段代码就是调用的接口模块了, @ApiImplicitParam()传进来的是参数,显示在页面上,required=true,是必填的,false是选填,paramType:表示参数放在哪个地方。

yml层

还记得我们在Swagger2类中注解吗:
@ConditionalOnProperty(prefix = “swagger”,value = {“enable”},havingValue = “true”)
我们需要在yml层配置,如果enable为true的情况下http://localhost:8080/swagger-ui.html页面才能正常访问,false那就不能进入了,我们在生产环境下是一定要关闭的,否者会被别有用心哦。

swagger:
	enable: true

到这里简单的demo就完成了我们看下页面,浏览器访问http://localhost:8080/swagger-ui.html(提示:地址中的8080,为你自己项目的端口号记得改变)

## KaTeX数学公式

当进入这个页面时就小功告成了。我们输入对应的数据点击下方的 Try it out!就能调取接口输出数据啦。

(由于本人是个比较懒的人这是个人CSDN第一篇博客,哈哈哈,后面会坚持更新自己学到的技术和处理的BUG,发现记在自己本上的技术,本子老丢哪哈哈哈)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值