Springboot—整合swagger2

前言:swagger2功能非常强大,用自己的一句话概括:它是一个构建强大的RESTful API文档以及调试的框架。

官方地址:https://swagger.io/

springboot整合非常简单,易学易用,步骤如下:

 

1、pom.xml所需依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>

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

 

2、配置swagger2

新建Swagger2Config.java文件,在扫包范围内

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).produces(Sets.newHashSet("application/json"))
                .consumes(Sets.newHashSet("application/json")).protocols(Sets.newHashSet("http", "https"))
                .apiInfo(apiInfo()).forCodeGeneration(true).useDefaultResponseMessages(false).select()
                // 指定controller存放的目录路径
                .apis(RequestHandlerSelectors.basePackage("cn.fzy.controller")).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 文档标题
                .title("swagger2测试api文档")
                // 文档描述
                .description("这里填写API接口文档简要描述")
                // 系统版本号
                .version("v1").license("MIT 协议").licenseUrl("http://www.opensource.org/licenses/MIT")
                // 联系人姓名、网址、邮箱
                .contact(new Contact("fzy", "Http://www.fengziy.cn", "fengziy@aliyun.com")).build();
    }
}

 

3、配置静态资源路径映射

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

 

4、管理RESTful接口

@Api(tags = { "测试接口" })
@RestController
public class TestController {

    @ApiOperation(value = "查询接口")
    @GetMapping("/test/{id}")
    public AjaxResult find(Integer id) {
        // TODO:查询
        return AjaxResult.success("查询结果");
    }

    @ApiOperation(value = "增加接口")
    @PostMapping("/test")
    public AjaxResult add(Object test) {
        // TODO:增加
        return AjaxResult.success("添加成功");
    }

    @ApiOperation(value = "删除接口")
    @DeleteMapping("/test/{id}")
    public AjaxResult delete(Integer id) {
        // TODO:删除
        return AjaxResult.success("删除成功");
    }

    @ApiOperation(value = "修改接口")
    @PutMapping("/text")
    public AjaxResult update(Integer id) {
        // TODO:修改
        return AjaxResult.success("修改成功");
    }
}

 

5、查看生成的api文档

 

 6、调试接口

 

 

总结:是不是很简单,这里只是简单的展示了方法,它还有更多的注解来描述接口、参数、返回值 。需要学习更多的可以到官网阅读文档。

希望对大家有所微薄的帮助。

 

                                                                                                 向上的路并不拥挤,到多数人选择了安逸!--it疯子也

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值