栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用Swagger实现接口文档

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Swagger实现接口文档

文章目录

一、简介二、配置Swagger

1、添加Swagger依赖2、创建Swagger配置类 三、访问http://localhost:8090/swagger-ui.html/

一、简介

在项目开发中,一般都是前后端分离,所有前后端工程师需要共同定义接口,编写接口文档,之后根据文档进行开发和维护。
为了编写和维护稳定,可以使用Swagger来编写API接口文档,提高效率

二、配置Swagger 1、添加Swagger依赖
  
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
2、创建Swagger配置类

在spirng boot集成时,放在与springbootApplication的同级目录下

package com.atmae.agriculture;

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.spi.documentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class Swagger2 {

    
    @Bean
    public Docket createRestApi(){
        return new Docket(documentationType.SPRING_WEB)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.atmae.agriculture.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("系统开发 API")
                .description(("这是系统开发API 前后端共同定义"))
                .termsOfServiceUrl("http://localhost:8090/")
                .version("1.0")
                .build();
    }
}

注意:如果报一下错误,则说明你的Springboot 版本过高。
笔者使用的 2.6.4 换成了 2.5.1 错误就没了

org.springframework.context.ApplicationContextException: Failed to start bean 
'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
三、访问http://localhost:8090/swagger-ui.html/

端口号根据自己的程序来确定。
注意:如果设置了访问权限 一定要给swagger的资源放行



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/758839.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号