Spring Boot集成Swagger

Swagger

Swagger,可以动态生成接口文档,方便测试与对外沟通。

使用

1.添加依赖

		<!-- swagger2核心包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- swagger-ui 可视化界面 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

2.添加SwaggerConfig配置文件

package com.chimy.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 // 开启Swagger的使用
public class SwaggerConfig {
    @Bean // Swagger的使用主要是要将docket对象传入IOC容器
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) //关于文档的各种信息
                .enable(true) //使Swagger生效
                .select()//选择扫描的接口
                .apis(RequestHandlerSelectors.basePackage("com.chimy.springboot.controller"))//指定扫描的接口
                .build();
    }

    public ApiInfo apiInfo(){
        //个人的联系方式
        Contact contact = new Contact("Chimy","https://blog.csdn.net/chimycmy?spm=1001.2101.3001.5343","demo@qq.com");
        //文档的各种信息
        return new ApiInfo("demo开发文档", "chimy的开发文档", "1.0", "urn:tos",null, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
    }
}

3.在接口添加注解描述等信息

package com.chimy.springboot.controller;

import com.chimy.springboot.bean.Author;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api("hello接口")
@RestController
public class HelloController {

    @Resource
    private Author author;

    @ApiOperation("测试接口")
    @GetMapping("/hello")
    public String hello(){
        return "hello world " + author.getName() + author.getUrl();
    }
}

效果

登录地址+/swagger-ui.html
http://localhost:8989/swagger-ui.html

在这里插入图片描述
参考:https://baijiahao.baidu.com/s?id=1689028738781297410&wfr=spider&for=pc
同事推荐的接口管理平台:yapi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值