spring2.6.7集成swagger

  1. 新建springboot项目,版本选择2.6.7
  2. pom文件添加swagger,版本为2.9.2
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.7</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.yf</groupId>
  <artifactId>security-oauth2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>security-oauth2</name>
  <description>Demo project for Spring Boot</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.security.oauth</groupId>
      <artifactId>spring-security-oauth2</artifactId>
      <version>2.3.6.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.security.oauth.boot</groupId>
      <artifactId>spring-security-oauth2-autoconfigure</artifactId>
      <version>2.1.2.RELEASE</version>
    </dependency>

    <!--引入swagger-->
    <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>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.18</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

  1. 添加swagger配置类
package com.yf.securityoauth2.config;

import io.swagger.annotations.Api;
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 yangfeng
 * @version 1.0
 * @date 2022-05-05 13:43
 */
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
  @Bean
  public Docket createRestApi(){
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
        .apis(RequestHandlerSelectors.basePackage("com.yf.securityoauth2.controller"))
        .paths(PathSelectors.any())
        .build();
  }

  //基本信息的配置,信息会在api文档上显示
  private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
        .title("spring security测试的接口文档")
        .description("用于描述spring security接口的详细文档")
        .termsOfServiceUrl("http://www.baidu.com")
        .version("1.0")
        .build();
  }

}

  1. 添加配置文件
server:
  port: 8012
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
# 防止访问控制台时报参数类型转换异常
logging:
  level:
    io:
      swagger:
        models:
          parameters:
            AbstractSerializableParameter: error

  1. 注解用法
    5.1@Api 用于标识类,表示该类是Swagger的资源
    在这里插入图片描述
    5.2@ApiIgnore 用于表示类,值忽略对该类的扫描
    5.3@ApiOperation 用于方法,描述 Controller类中的 method接口
    5.4@ApiParam: 用于参数,单个参数描述,与 @ApiImplicitParam不同的是,他是写在参数左侧的。如( @ApiParam(name=“username”,value=“用户名”)Stringusername)
    5.5@ApiModel: 用于类,表示对类进行说明,用于参数用实体类接收
    5.6@ApiProperty:用于方法,字段,表示对model属性的说明或者数据操作更改
    5.7@ApiImplicitParam: 用于方法,表示单独的请求参数
    5.8@ApiImplicitParams: 用于方法,包含多个 @ApiImplicitParam
    在这里插入图片描述

5.9@ApiResponse: 用于方法,描述单个出参信息
5.10@ApiResponses: 用于方法,包含多个@ApiResponse
5.10@ApiError: 用于方法,接口错误所返回的信息

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值