spring-boot2.7.8添加swagger的案例詳解
一、新建項(xiàng)目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 = "保存產(chǎn)品")
@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;
}運(yùn)行效果:

到此這篇關(guān)于spring-boot2.7.8添加swagger的文章就介紹到這了,更多相關(guān)spring-boot2.7.8添加swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微服務(wù)進(jìn)行遠(yuǎn)程調(diào)用其他服務(wù)方式
文章主要介紹了如何將本地服務(wù)注冊到Nacos地址上,并詳細(xì)描述了服務(wù)調(diào)用和負(fù)載均衡的實(shí)現(xiàn)方法,包括使用`@EnableDiscoveryClient`注解開啟服務(wù)調(diào)用、編寫調(diào)用接口、創(chuàng)建RestTemplate配置類以及使用負(fù)載均衡依賴進(jìn)行服務(wù)調(diào)用2026-01-01
tio-boot框架整合ehcache實(shí)現(xiàn)過程示例
這篇文章主要為大家介紹了tio-boot框架整合ehcache實(shí)現(xiàn)過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
SpringBoot整合Swagger3生成接口文檔的示例代碼
Swagger 是一個(gè) RESTful API 的開源框架,它的主要目的是幫助開發(fā)者設(shè)計(jì)、構(gòu)建、文檔化和測試 Web API,本文給大家介紹了SpringBoot整合Swagger3生成接口文檔的流程,并通過代碼講解的非常詳細(xì),需要的朋友可以參考下2024-04-04
Java中保留兩位小數(shù)的四種方法實(shí)現(xiàn)實(shí)例
今天小編就為大家分享一篇關(guān)于Java中保留兩位小數(shù)的四種方法實(shí)現(xiàn)實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
SpringBoot實(shí)現(xiàn)接口冪等性的4種方案
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)接口冪等性的4種方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

