1、引入 Swagger3 依赖
<!-- 引入Swagger3依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2、编写 Swagger3 配置文件
/**
* Swagger配置类
*
* @author Strive
*/
@Configuration
@EnableOpenApi
public class Swagger3Config {
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.enable(true)
.select()
// apis: 添加swagger接口提取范围
.apis(RequestHandlerSelectors.basePackage("com.csp.mingyue.swagger3.controller"))
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3 测试接口文档")
.description("【接口篇】SpringBoot 整合 Swagger3 实现在线 API 文档")
.contact(
new Contact(
"Strive", "https://gitee.com/csps/mingyue-springboot-learning", "732171109@qq.com"))
.version("3.0")
.build();
}
}
3、编写接口
- config
package com.csp.mingyue.swagger3.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* Swagger配置类
*
*/
@Configuration
@EnableOpenApi
public class Swagger3Config {
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.enable(true)
.select()
// apis: 添加swagger接口提取范围
.apis(RequestHandlerSelectors.basePackage("com.csp.mingyue.swagger3.controller"))
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3 测试接口文档")
.description("【接口篇】SpringBoot 整合 Swagger3 实现在线 API 文档")
.contact(
new Contact(
"Strive", "https://gitee.com/csps/mingyue-springboot-learning", "732171109@qq.com"))
.version("3.0")
.build();
}
}
- controller
package com.csp.mingyue.swagger3.controller;
import cn.hutool.core.util.StrUtil;
import com.csp.mingyue.swagger3.model.MingYueUser;
import com.csp.mingyue.swagger3.service.MingYueUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "用户模块")
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class MingYueUserController {
private final MingYueUserService mingYueUserService;
@ApiOperation("根据用户ID查询用户信息")
@GetMapping("/{userId}")
public ResponseEntity<MingYueUser> queryUserById(@PathVariable Long userId) {
return ResponseEntity.ok(mingYueUserService.queryUserById(userId));
}
@ApiOperation("添加用户")
@PostMapping
public ResponseEntity<Long> addUser(@RequestBody MingYueUser user) throws Exception {
if (null == user.getUserId()) {
throw new Exception("userId 不能为空!");
}
if (StrUtil.isBlank(user.getUsername())) {
throw new Exception("username 不能为空!");
}
return ResponseEntity.ok(mingYueUserService.addUser(user));
}
@ApiOperation("更新用户")
@PutMapping
public ResponseEntity<String> updateUser(@RequestBody MingYueUser user) {
return ResponseEntity.ok(mingYueUserService.updateUser(user));
}
@ApiOperation("根据用户ID删除用户")
@DeleteMapping("/{userId}")
public ResponseEntity<String> deleteUser(@PathVariable Long userId) {
return ResponseEntity.ok(mingYueUserService.deleteUser(userId));
}
}
- model
package com.csp.mingyue.swagger3.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "用户实体类", description = "用户信息描述类")
public class MingYueUser {
@ApiModelProperty(value = "用户id")
private Long userId;
@ApiModelProperty(value = "用户名")
private String username;
}
- service
package com.csp.mingyue.swagger3.service;
import cn.hutool.core.map.MapUtil;
import com.csp.mingyue.swagger3.model.MingYueUser;
import java.util.Map;
import org.springframework.stereotype.Service;
@Service
public class MingYueUserService {
/** 模拟用户存储 */
private static final Map<Long, MingYueUser> USER_MAP = MapUtil.newHashMap();
static {
USER_MAP.put(1L, MingYueUser.builder().userId(1L).username("mingyue").build());
}
/**
* 根据用户ID查询用户信息
*
* @param userId 用户ID
* @return 用户信息
*/
public MingYueUser queryUserById(Long userId) {
return USER_MAP.get(userId);
}
/**
* 添加用户
*
* @param user 用户信息
* @return 新用户ID
*/
public Long addUser(MingYueUser user) {
USER_MAP.put(2L, user);
return user.getUserId();
}
/**
* 更新用户
*
* @param user 用户信息
*/
public String updateUser(MingYueUser user) {
MingYueUser mingYueUser = USER_MAP.get(user.getUserId());
USER_MAP.put(user.getUserId(), user);
return mingYueUser.getUsername() + " 更新为:" + user.getUsername();
}
/**
* 根据用户ID删除用户
*
* @param userId 用户ID
*/
public String deleteUser(Long userId) {
int size = USER_MAP.size();
USER_MAP.remove(userId);
return "原" + size + "用户,删除后还有" + USER_MAP.size();
}
}
来源:https://blog.csdn.net/csp732171109/article/details/124197693
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END