java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > spring-boot2.7.8添加swagger

spring-boot2.7.8添加swagger的案例详解

作者:大熊程序猿

这篇文章主要介绍了spring-boot2.7.8添加swagger的案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

一、新建项目swaggerdemo

二、修改pom.xml

注意修改:spring-boot-starter-parent版本为:2.7.8

添加依赖:

springfox-swagger2
springfox-swagger-ui
springfox-boot-starter

<?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.7.8</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.saas</groupId>
    <artifactId>swaggerdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>com.saas.swaggerdemo</name>
    <description>com.saas.swaggerdemo</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>aliyunmaven</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
        <repository>
            <id>central2</id>
            <name>central2</name>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>
</project>

修改:application.yml

matching-strategy: ant_path_matcher

server:
  port: 81
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

添加SwaggerConfig

package com.saas.swaggerdemo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@EnableOpenApi
@Configuration
public class SwaggerConfig {
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger Test App Restful API")
                .description("swagger test app restful api")
                .version("1.0.0.0")
                .build();
    }
    @Bean
    public Docket createRestApi(ApiInfo apiInfo) {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo)
                .groupName("SwaggerGroupOneAPI")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build();
    }
}

二、添加控器器

ProductController.java

package com.saas.swaggerdemo.controllers;
import com.saas.swaggerdemo.ProductDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
@RestController
@Api(value = "ProductController")
@RequestMapping("/products")
public class ProductController {
    @ApiOperation(value = "保存产品")
    @PostMapping("save")
    public boolean SaveProduct(@RequestBody ProductDto productDto) {
        return true;
    }
}

ProductDto.java 

package com.saas.swaggerdemo;
public class ProductDto {
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private  String name;
}

运行效果:

到此这篇关于spring-boot2.7.8添加swagger的文章就介绍到这了,更多相关spring-boot2.7.8添加swagger内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文