一、新建项目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 {@Beanpublic ApiInfo apiInfo() {return new ApiInfoBuilder().title("Swagger Test App Restful API").description("swagger test app restful api").version("1.0.0.0").build();}@Beanpublic 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;
}
运行效果: