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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Springboot+Mybatis對數(shù)據(jù)訪問層進行單元測試的方式分享
本文將介紹一種快高效、可復用的解決測試方案——對數(shù)據(jù)訪問層做單元測試,文章通過代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-07-07SpringBoot集成WebSocket實現(xiàn)后臺向前端推送信息的示例
這篇文章主要介紹了SpringBoot集成WebSocket實現(xiàn)后臺向前端推送信息的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12Java8函數(shù)式接口java.util.function速查大全
因為Java8引入了函數(shù)式接口,在java.util.function包含了幾大類函數(shù)式接口聲明,這篇文章主要給大家介紹了關于Java8函數(shù)式接口java.util.function速查的相關資料,需要的朋友可以參考下2021-08-08