SpringCloud学习笔记(一)准备

1、要注意SpringCloud和SpringBoot的版本对应关系,可以通过https://start.spring.io/actuator/info获取

2、swagger版本和SpringBoot之间要对应,当前SpringBoot版本号为2.5.6,swagger2版本号为2.9.2,SpringCloud版本号为2020.0.5,swagger2的添加方式如下:

  1)pom.xml中添加以下dependency

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

  2)Application.java中添加以下注解,并添加Swagger2AutoConfiguration类和Swagger2Properties类:

@EnableSwagger2
@Import(Swagger2AutoConfiguration.class)
package com.nmhr.tools.service.starter.autoconfigure;

import com.nmhr.tools.service.starter.data.properties.Swagger2Properties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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 java.sql.Timestamp;

@Configuration
@EnableConfigurationProperties(Swagger2Properties.class)
public class Swagger2AutoConfiguration {

    /**
     * swagger2属性
     */
    private final Swagger2Properties swagger2Properties;

    /**
     * 构造函数,注入swagger2属性
     *
     * @param swagger2Properties
     */
    public Swagger2AutoConfiguration(Swagger2Properties swagger2Properties) {
        this.swagger2Properties = swagger2Properties;
    }

    /**
     * swagger构建器
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(this.swagger2Properties.getEnable())
                .apiInfo(this.getApiInfo())
                .select() //指定需要发布到Swagger的接口目录,不支持通配符
                .apis(RequestHandlerSelectors.basePackage(this.swagger2Properties.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(Timestamp.class, String.class);
    }

    /**
     * 获取api信息
     *
     * @return
     */
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title(this.swagger2Properties.getTitle())
                .description(this.swagger2Properties.getDescription())
                .termsOfServiceUrl(this.swagger2Properties.getTermsOfServiceUrl())
                .version(this.swagger2Properties.getVersion())
                .build();
    }
}
package com.nmhr.tools.service.starter.data.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("swagger2")
@Getter
@Setter
public class Swagger2Properties {
    /**
     * 文档标题
     */
    private String title;

    /**
     * 文档描述
     */
    private String description;

    /**
     * 服务条款网址
     */
    private String termsOfServiceUrl;

    /**
     * 是否开启
     */
    private Boolean enable;

    /**
     * 文档版本
     */
    private String version;

    /**
     * api接口包扫描路径
     */
    private String basePackage;
}

  3)application.yml中设置swagger2属性:

swagger2:
  title: 微服务APIs
  description: 微服务APIs包含XXX模块的对外接口
  terms-of-service-url:
  enable: true
  version: @version@
  base-package: com.nmhr.service.system.controller.api

 

3、通过dependencyManagement控制各个子模块的SpringCloud版本,子模块中的org.springframework.cloud不用写版本号

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

上一篇:SpringBoot集成与配置Swagger2


下一篇:swagger2的使用java集成