Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c39d559

Browse files
author
YunaiV
committed
增加 Swagger Starter 的示例
1 parent ce180d3 commit c39d559

File tree

9 files changed

+265
-0
lines changed

9 files changed

+265
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.11.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-24-apidoc-swagger-starter</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring MVC 的自动配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
<!-- 实现对 Swagger 的自动配置 -->
23+
<dependency>
24+
<groupId>io.springfox</groupId>
25+
<artifactId>springfox-boot-starter</artifactId>
26+
<version>3.0.0</version>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab24;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.iocoder.springboot.lab24.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import springfox.documentation.builders.ApiInfoBuilder;
5+
import springfox.documentation.builders.PathSelectors;
6+
import springfox.documentation.builders.RequestHandlerSelectors;
7+
import springfox.documentation.service.ApiInfo;
8+
import springfox.documentation.service.Contact;
9+
import springfox.documentation.spi.DocumentationType;
10+
import springfox.documentation.spring.web.plugins.Docket;
11+
12+
//@Configuration
13+
// @EnableSwagger2 无需使用该注解
14+
public class SwaggerConfiguration {
15+
16+
@Bean
17+
public Docket createRestApi() {
18+
// 创建 Docket 对象
19+
return new Docket(DocumentationType.SWAGGER_2) // 文档类型,使用 Swagger2
20+
.apiInfo(this.apiInfo()) // 设置 API 信息
21+
// 扫描 Controller 包路径,获得 API 接口
22+
.select()
23+
.apis(RequestHandlerSelectors.basePackage("cn.iocoder.springboot.lab24.controller"))
24+
.paths(PathSelectors.any())
25+
// 构建出 Docket 对象
26+
.build();
27+
}
28+
29+
/**
30+
* 创建 API 信息
31+
*/
32+
private ApiInfo apiInfo() {
33+
return new ApiInfoBuilder()
34+
.title("测试接口文档示例")
35+
.description("我是一段描述")
36+
.version("1.0.0") // 版本号
37+
.contact(new Contact("芋艿", "http://www.iocoder.cn", "zhijiantianya@gmail.com")) // 联系人
38+
.build();
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package cn.iocoder.springboot.lab24.controller;
2+
3+
import cn.iocoder.springboot.lab24.dto.UserAddDTO;
4+
import cn.iocoder.springboot.lab24.dto.UserUpdateDTO;
5+
import cn.iocoder.springboot.lab24.vo.UserVO;
6+
import io.swagger.annotations.Api;
7+
import io.swagger.annotations.ApiImplicitParam;
8+
import io.swagger.annotations.ApiOperation;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.UUID;
14+
15+
@RestController
16+
@RequestMapping("/users")
17+
@Api(tags = "用户 API 接口")
18+
public class UserController {
19+
20+
@GetMapping("/list")
21+
@ApiOperation(value = "查询用户列表", notes = "目前仅仅是作为测试,所以返回用户全列表")
22+
public List<UserVO> list() {
23+
// 查询列表
24+
List<UserVO> result = new ArrayList<>();
25+
result.add(new UserVO().setId(1).setUsername("yudaoyuanma"));
26+
result.add(new UserVO().setId(2).setUsername("woshiyutou"));
27+
result.add(new UserVO().setId(3).setUsername("chifanshuijiao"));
28+
// 返回列表
29+
return result;
30+
}
31+
32+
@GetMapping("/get")
33+
@ApiOperation("获得指定用户编号的用户")
34+
@ApiImplicitParam(name = "id", value = "用户编号", paramType = "query", dataTypeClass = Integer.class, required = true, example = "1024")
35+
public UserVO get(@RequestParam("id") Integer id) {
36+
// 查询并返回用户
37+
return new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
38+
}
39+
40+
@PostMapping("add")
41+
@ApiOperation("添加用户")
42+
public Integer add(UserAddDTO addDTO) {
43+
// 插入用户记录,返回编号
44+
Integer returnId = UUID.randomUUID().hashCode();
45+
// 返回用户编号
46+
return returnId;
47+
}
48+
49+
@PostMapping("/update")
50+
@ApiOperation("更新指定用户编号的用户")
51+
public Boolean update(UserUpdateDTO updateDTO) {
52+
// 更新用户记录
53+
Boolean success = true;
54+
// 返回更新是否成功
55+
return success;
56+
}
57+
58+
@PostMapping("/delete")
59+
@ApiOperation(value = "删除指定用户编号的用户")
60+
@ApiImplicitParam(name = "id", value = "用户编号", paramType = "query", dataTypeClass = Integer.class, required = true, example = "1024")
61+
public Boolean delete(@RequestParam("id") Integer id) {
62+
// 删除用户记录
63+
Boolean success = false;
64+
// 返回是否更新成功
65+
return success;
66+
}
67+
68+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.iocoder.springboot.lab24.dto;
2+
3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
5+
6+
@ApiModel("用户添加 DTO")
7+
public class UserAddDTO {
8+
9+
@ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma")
10+
private String username;
11+
@ApiModelProperty(value = "密码", required = true, example = "nicai")
12+
private String password;
13+
14+
public String getUsername() {
15+
return username;
16+
}
17+
18+
public UserAddDTO setUsername(String username) {
19+
this.username = username;
20+
return this;
21+
}
22+
23+
public String getPassword() {
24+
return password;
25+
}
26+
27+
public UserAddDTO setPassword(String password) {
28+
this.password = password;
29+
return this;
30+
}
31+
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.iocoder.springboot.lab24.dto;
2+
3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
5+
6+
@ApiModel("用户更新 DTO")
7+
public class UserUpdateDTO {
8+
9+
@ApiModelProperty(value = "用户编号", required = true, example = "1024")
10+
private Integer id;
11+
@ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma")
12+
private String username;
13+
@ApiModelProperty(value = "密码", required = true, example = "nicai")
14+
private String password;
15+
16+
public Integer getId() {
17+
return id;
18+
}
19+
20+
public UserUpdateDTO setId(Integer id) {
21+
this.id = id;
22+
return this;
23+
}
24+
25+
public String getUsername() {
26+
return username;
27+
}
28+
29+
public UserUpdateDTO setUsername(String username) {
30+
this.username = username;
31+
return this;
32+
}
33+
34+
public String getPassword() {
35+
return password;
36+
}
37+
38+
public UserUpdateDTO setPassword(String password) {
39+
this.password = password;
40+
return this;
41+
}
42+
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.iocoder.springboot.lab24.vo;
2+
3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
5+
6+
@ApiModel("用户 VO")
7+
public class UserVO {
8+
9+
@ApiModelProperty(value = "用户编号", required = true, example = "1024")
10+
private Integer id;
11+
@ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma")
12+
private String username;
13+
14+
public Integer getId() {
15+
return id;
16+
}
17+
18+
public UserVO setId(Integer id) {
19+
this.id = id;
20+
return this;
21+
}
22+
23+
public String getUsername() {
24+
return username;
25+
}
26+
27+
public UserVO setUsername(String username) {
28+
this.username = username;
29+
return this;
30+
}
31+
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 对应 SpringfoxConfigurationProperties 配置类
2+
springfox:
3+
documentation:
4+
swagger-ui:
5+
enabled: true # 是否开启 Swagger UI 功能。默认为 true

‎lab-24/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>lab-24-apidoc-swagger</module>
1616
<module>lab-24-apidoc-swagger-knife4j</module>
1717
<module>lab-24-apidoc-japidocs</module>
18+
<module>lab-24-apidoc-swagger-starter</module>
1819
</modules>
1920

2021

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /