栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

common包:SpringBoot整合Swagger3

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

common包:SpringBoot整合Swagger3

目录

1.父工程Pom文件

2.common-swagger模块

2.1 Pom文件

2.2 SwaggerProperties文件

2.3 SwaggerConfig文件

2.4 spring.factories文件

3.csdn-test项目测试swagger

3.1 pom文件

3.2 application.yml文件

3.3 CsdnTestApplication文件

3.4 TestController文件

4.启动项目,看效果


项目结构

1.父工程Pom文件


    4.0.0
    com.mcs
    CSDN
    pom
    1.0
    
    
        csdn-common
        csdn-test
    

    
        1.0
        1.8
        8
        8
        UTF-8
        UTF-8
        2.3.7.RELEASE
        3.0.0
    

    
        
            
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            

            
            
                io.springfox
                springfox-boot-starter
                ${swagger.fox.version}
            

            
            
                com.mcs
                common-swagger
                ${csdn.version}
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    ${java.version}
                    ${java.version}
                    ${project.build.sourceEncoding}
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    

2.common-swagger模块

2.1 Pom文件


    
        com.mcs
        csdn-common
        1.0
    
    4.0.0
    common-swagger

    
        
            io.springfox
            springfox-boot-starter
        
        
            org.springframework.boot
            spring-boot-autoconfigure
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-configuration-processor
        
    

2.2 SwaggerProperties文件
package com.mcs.common.swagger.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConfigurationProperties(prefix = "springfox.documentation.swagger")
public class SwaggerProperties {

    
    private String basePackage;

    
    private String title;

    
    private String description;

    
    private String version;

    
    private String contactName;

    
    private String contactUrl;

    
    private String contactEmail;

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactUrl() {
        return contactUrl;
    }

    public void setContactUrl(String contactUrl) {
        this.contactUrl = contactUrl;
    }

    public String getContactEmail() {
        return contactEmail;
    }

    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }
}

2.3 SwaggerConfig文件
package com.mcs.common.swagger.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@Configuration
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerConfig { 
    
    private SwaggerProperties properties;

    @Autowired
    public void setProperties(SwaggerProperties properties) {
        this.properties = properties;
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo(properties))
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(SwaggerProperties properties) {
        return new ApiInfoBuilder()
                .title(properties.getTitle())
                .description(properties.getDescription())
                .contact(new Contact(properties.getContactName(), properties.getContactUrl(), properties.getContactEmail()))
                .version(properties.getVersion())
                .build();
    }
}

swagger3与swagger2不同,开启swagger3的注解是@EnableOpenApi。swagger3默认是开启的,所以@EnableOpenApi注解可不写。

2.4 spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  com.mcs.common.swagger.config.SwaggerConfig

3.csdn-test项目测试swagger

3.1 pom文件


    
        com.mcs
        CSDN
        1.0
    
    4.0.0
    csdn-test

    
        
            com.mcs
            common-swagger
        
    

3.2 application.yml文件
server:
  port: 8080

spring:
  application:
    name: csdn-test

springfox:
  documentation:
    # swagger3开关位置,默认是开启的
    enabled: true
    swagger:
      title: test-swagger
      description: "测试swagger"
      version: 1.0.0
      contactName: mcs
      contactEmail: xxx@qq.com
      contactUrl: http://xxx.com

swagger3通过springfox.documentation.enabled控制开关;swagger2可以通过@ConditionalOnProperty注解控制。

3.3 CsdnTestApplication文件
package com.mcs.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

}

3.4 TestController文件
package com.mcs.test.controller;

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

@RestController
@Api(tags = "测试Swagger")
public class TestController {

    @ApiOperation(value = "测试Swagger")
    @PostMapping("/test")
    public String test() {
        return "test";
    }
}

4.启动项目,看效果

访问路径http://localhost:8080/swagger-ui/index.html

 注意,swagger3的访问路径与swagger2是不同的。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871551.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号