卜若的代码笔记系列-Web系列-SpringBoot-第十一章:Swagger环境搭建-3211


背景:我们遇到一个问题,在团队开发的时候,我们需要共享api
信息(api,也就是应用程序接口,之前主城讲那你写个什么什么
接口,其实讲的就是去实现这样一个api,but,我之前一直理解为时
函数,emmmm,这就很尴尬了)。然后怎么共享api,这是一个问题,
所以,在这个问题的背景下,我们提出了swagger

ps:嘤嘤嘤?拜托,网上的大神们,能不能写详细点,真的好难找,不想写详细点就别写好吗,怕您累着。

 

1.首先,我们想使用的swagger是一个插件,所以我们要通过maven去添加相关依赖。

在pom里面添加

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

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

这样maven会自动下载一些swagger的相关依赖。

2.需要注册这个swagger的相关信息,怎么注册呢,当然是创建一个类,

通过@Configuration这个标签去注册

通过@EnableSwagger2去启用所有的swagger

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 SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("brTest.example.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot&Swagger")
                .description("这是一个swagger的测试")
                .termsOfServiceUrl("https://blog.csdn.net/qq_37080133")
                .version("1.0")
                .build();
    }
}

 3.你注册了这个信息之后,你得去启用,真的是感谢大神的博客,我找了两个小时,心态炸了,网上千篇一律,真特么恶心,你特么抄也抄详细点行吗?

https://blog.csdn.net/u013128651/article/details/78864876
 


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
@EnableWebMvc

public class Swagger2Config
{

    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");

            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");

        }
    }

        
    
}

4.这样,如果你还进不去你就留言 ,我帮你解决,最近我一直在有空

5.大佬们,真的,你不想写详细点让菜鸟能够看得懂就别写,会的人不看,好气,浪费时间是吗?

//因为直接复制黑框文本到eclipse会直接报错,我这里再写一份代码

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值