- 文件|新建|项目,弹出如下对话框

- 点击下一步后

- 点击完成后自动导包
- 加入以下两个swagger依赖

完整的pom文件如下:
<?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>
<groupId>com.example</groupId>
<artifactId>webapitest1_4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webapitest1_4</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.6</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.8</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.webapitest1_4.Webapitest14Application</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 在com.example.webapitest1_4下新增以下文件夹 config,controller、entity三个文件夹,同时在controller创建一个basic文件夹,如下图所示

- 在config 目录下新建一个SwaggerConfig 类,内容如下
package com.example.webapitest1_4.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public Docket basicDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("test")
.enable(true)
.apiInfo(basicApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.webapitest1_4.controller.basic"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo basicApiInfo(){
Contact contact=new Contact("XXXX","https://www.baidu.com/","qq.com");
return new ApiInfo(
"test",
"测试",
"V1.0",
"url",
contact,
"",
"",
new ArrayList<>());
}
}
注意:@EnableSwagger2的依赖是 springfox.documentation.swagger2.annotations
- 在entity 目录下新建 User 类,内容如下
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.webapitest1_4.entity;
import lombok.Data;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@Data
public class User {
private String name;
private Integer age;
}
- 在controller 目录下新增BasicController 类,内容如下
package com.example.webapitest1_4.controller.basic;
import com.example.webapitest1_4.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@Controller
public class BasicController {
@RequestMapping("/user")
@ResponseBody
public User user() {
User user = new User();
user.setName("theonefx");
user.setAge(666);
return user;
}
}
- 配置application.properties 目录,内容如下
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.example.webapitest1_4.mybatis.entity
# 应用服务 WEB 访问端口
server.port=8080
spring.datasource.url= oracle数据库连接信息
spring.datasource.username= 用户名
spring.datasource.password= 密码
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
- 打开网页 swagger网页

本文介绍了如何在SpringBoot项目中集成Swagger2进行API文档生成,包括添加Swagger依赖、创建配置类SwaggerConfig、定义API信息和控制器,以及配置application.properties文件。
4224

被折叠的 条评论
为什么被折叠?



