Springboot 引入Swagger2

前后端分离模式下,引用Swagger2有效可以解决手写Api文档

一、pom.xml引入依赖

        <!-- Swagger核心包 start -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- Swagger核心包 end -->

二、Swagger配置类

package com.*.*.configuration;

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;

/**
 * @author debao
 * 
 */

@Configuration
@EnableSwagger2
public class Swagger2{
        /**
         * 通过 createRestApi函数来构建一个DocketBean
         * 函数名,可以随意命名,喜欢什么命名就什么命名
         */

        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)

                    .apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                    .select()
                    //控制暴露出去的路径下的实例
                    //如果某个接口不想暴露,可以使用以下注解
                    //@ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
                    .apis(RequestHandlerSelectors.basePackage("com.*.*.controller")) //扫描控制类的路径
                    .paths(PathSelectors.any())
                    .build();

        }

        //构建 api文档的详细信息函数
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    //页面标题
                    .title("user服务端提供者接口平台")
                    //条款地址
                    .termsOfServiceUrl("http://despairyoke.github.io/")
                    .contact("技术开发部")
                    .version("1.0")
                    //描述
                    .description("API 服务相关数据接口")
                    .build();
        }
}

启动类 加上注解@EnableSwagger2 表示开启Swagger

package com.*.*;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class CustomerApplication {
    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class, args);

    }
}

控制类Controller增加Swagger2的注解来说明接口的信息

  1. @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")

  2. @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")

其实不需要加上API注解也可以正常使用,启动SpringBoot项目,访问 http://localhost:8080/swagger-ui.html

访问路径时找不到,报404,可能是配置冲突,排除是否访问静态资源路径配置冲突

解决:

通过WebMvcConfigurer,实现addResourceHandlers方法,设置静态资源可访问

package com.*.*.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 初始配置
 *
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    //访问静态资源
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决swagger无法访问
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
}

三、Swagger2相关注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

  • @Api:修饰整个类,描述Controller的作用
  • @ApiOperation:描述一个类的一个方法,或者说一个接口
  • @ApiParam:单个参数描述
  • @ApiModel:用对象来接收参数
  • @ApiProperty:用对象接收参数时,描述对象的一个字段
  • @ApiResponse:HTTP响应其中1个描述
  • @ApiResponses:HTTP响应整体描述
  • @ApiIgnore:使用该注解忽略这个API
  • @ApiError :发生错误返回的信息
  • @ApiImplicitParam:一个请求参数
  • @ApiImplicitParams:多个请求参数
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值