Swagger接口文档

目录

介绍

使用步骤

1.添加依赖

2.新建一个config包,写配置类

3.controller包下面随便写一个测试类

4.重启项目打开网页进入访问地址


介绍

Swagger 是一个规范且完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。简单来讲就是它可以根据所写的代码自动生成接口文档。可以进行接口调试。

使用步骤

1.添加依赖

<!--  swagger2 依赖  -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<!--  Swagger第三方ui依赖 因为swagger2原生的ui有点丑,所以就用了第三方的一个ui -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>

 上面使用了第三方依赖,目的是让界面变的更美观

2.新建一个config包,写配置类

package com.example.server.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;

@Configuration  //标记为配置类
@EnableSwagger2  //开启Swagger在线接口文档
public class Swagger2Config {
    /**
     * 添加摘要信息(Docket)
     * 
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.server.controller"))
                .paths(PathSelectors.any())//路径(任何)
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("云E办接口文档")
                .description("云E办接口文档")
                .contact(new Contact("Alva", "http://localhost:9191/doc.html", "2017865616@qq.com"))
                .version("1.0")
                .build();
    }
}

3.controller包下面随便写一个测试类



@RestController
public class HelloController {

    @GetMapping("hello")
    public String hello(){
        return "hello";
    }
}

4.重启项目打开网页进入访问地址

访问地址中的端口号要与yml中的端口号保持一致

http://localhost:9191/doc.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值