SpringBoot多模块使用Swagger2 3.0

  • Post author:
  • Post category:其他

在父项目里创建common模块

本次使用的是swagger2 的3.9版本,创建common模块来配置swagger是为后续多模块用swagger准备的。

pom.xml文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-eureka</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>swagger-config</module>
    </modules>

    <groupId>com.kevin</groupId>
    <artifactId>common</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
            <scope>provided </scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
            <scope>provided </scope>
        </dependency>

    </dependencies>
</project>

在common模块里再创建子模块swaggerconfig

  • 建立SwaggerConfig配置类,这里给出简单的配置,仅供参考
package com.kevin.swaggerconfig;

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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration //配置类
//@EnableSwagger2  //引入Swagger注解
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("接口文档中心")
                .description("本文档描述了各个微服务接口定义")
                .version("1.0")
                .contact(new Contact("kevin", "http://baidu.com", "535223500@qq.com"))
                .build();
    }
}

使用swagger

  • 在需要使用到swagger的模块中的pom.xml文件里加入以下依赖
<!--导入swagger配置类的依赖,即common模块里的swaggercofnig子模块的配置类-->
		<dependency>
			<groupId>com.kevin</groupId>
			<artifactId>swagger-config</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<!--swagger-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>3.0.0</version>
			<scope>provided </scope>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-boot-starter</artifactId>
			<version>3.0.0</version>
		</dependency>
		<!--解决无法访问swagger-ui界面问题(404)-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>3.0.0</version>
		</dependency>

		<!--swagger增强版依赖  访问:http://localhost:8761/doc.html(ip+端口+doc.html)-->
		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>knife4j-spring-boot-starter</artifactId>
			<version>3.0.2</version>
		</dependency>
  • 在controller加入swagger相关注解
package com.example.eurekaproducer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author GAOSONG
 * @date 2021/7/9 10:33
 */
@RestController
@RequestMapping("employee")
@Api("员工管理")
public class EmployeeController {

    @ApiOperation(notes = "员工列表",value = "员工列表")
    @GetMapping("/listEmployee")
    public String listEmployee(String name){
        return name;
    }
}
  • 启动类加上@ComponentScan注解,主要是为了扫描到接口所在的位置
package com.example.eurekaproducer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = "com.example")
public class EurekaProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaProducerApplication.class, args);
    }

}

访问swagger-ui

  • 普通页面:
    http://localhost:8761/swagger-ui/index.html
    8761是我自己的端口,具体还是根据个人设置的端口好来访问
  • 增强版:
    http://localhost:8761/doc.html
    使用增强版需要导入依赖,以上pom.xml有说明。

版权声明:本文为qq_44940503原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。