spring boot项目应用一:接入swagger2

本文详细介绍如何在SpringBoot项目中集成Swagger2,包括添加依赖、配置文件、启动类及控制层注解,以便生成清晰的API文档,方便开发者理解和使用。

1.添加依赖:

		
<swaggwer.version>2.7.0</swaggwer.version>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swaggwer.version}</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swaggwer.version}</version>
</dependency>

2.配置swagger文件和启动文件存放在同一级下:

package com.huayu.shiro;

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.WebMvcConfigurerAdapter;
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;


/**
 * Created by huayu on 2018/8/20.
 */
@Configuration
@EnableSwagger2
@EnableWebMvc
public class Swagger2 extends WebMvcConfigurerAdapter {
    /**
     *  有时候出现加载异常,需要添加此方法。方便找到网页位置
     *  @EnableWebMvc
     *  extends WebMvcConfigurerAdapter
     *
     * @param registry
     */
    @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/");
    }


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.shiro"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Spring Boot 测试使用 构建RESTful API")
                //版本号
                .version("1.0")
                //描述
                .description("HuaYu")
                .build();
    }

}

3.在control层添加注解方便在API文档中查看接口描述:

关于注释自己看到一个描述很全面的文章:https://blog.youkuaiyun.com/xupeng874395012/article/details/68946676

添加完注解后就可以启动项目,访问项目:IP:端口/swagger-ui.html

 

 

 

未完待续

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值