Contents
新建spring boot工程
添加依赖
1 <dependency>
2 <groupId>io.springfox</groupId>
3 <artifactId>springfox-swagger2</artifactId>
4 <version>2.9.2</version>
5 </dependency>
6 <dependency>
7 <groupId>io.springfox</groupId>
8 <artifactId>springfox-swagger-ui</artifactId>
9 <version>2.9.2</version>
10 </dependency>
新建swagger配置
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4import springfox.documentation.builders.ApiInfoBuilder;
5import springfox.documentation.builders.PathSelectors;
6import springfox.documentation.builders.RequestHandlerSelectors;
7import springfox.documentation.service.ApiInfo;
8import springfox.documentation.service.Contact;
9import springfox.documentation.spi.DocumentationType;
10import springfox.documentation.spring.web.plugins.Docket;
11import springfox.documentation.swagger2.annotations.EnableSwagger2;
12
13/**
14 * @author felix
15 * @ 日期 2019-05-27 12:03
16 */
17@Configuration
18@EnableSwagger2
19public class SwaggerConfiguration {
20 @Value("${swagger.enabled}")
21 private boolean enable;
22
23 @Bean
24 public Docket createRestApi() {
25 return new Docket(DocumentationType.SWAGGER_2)
26 .apiInfo(apiInfo())
27 .enable(enable)
28 .select()
29 .apis(RequestHandlerSelectors.basePackage("com.fecred.villagedoctor.controller"))
30 .paths(PathSelectors.any())
31 .build();
32 }
33
34 @Bean
35 public Docket healthApi() {
36 return new Docket(DocumentationType.SWAGGER_2)
37 .groupName("分组名称")
38 .apiInfo(apiInfo())
39 .enable(enable)
40 .select()
41 //分组的controller层
42 .apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
43 .paths(PathSelectors.regex(".*/health/.*"))
44 .build();
45 }
46
47 private ApiInfo apiInfo() {
48 return new ApiInfoBuilder()
49 .title("标题")
50 .description("一般描述信息")
51 .termsOfServiceUrl("http://localhost:8999/")
52 .contact(new Contact("联系人", "邮箱", "邮箱"))
53 .version("1.0")
54 .build();
55 }
56}
新建前后端响应工具类
1import io.swagger.annotations.ApiModelProperty;
2import lombok.Data;
3
4/**
5 * @author ie
6 * @ 日期 2019-10-25 14:24
7 */
8@Data
9public class Result<T> {
10 @ApiModelProperty(value = "状态值")
11 private int code;
12 @ApiModelProperty(value = "提示信息")
13 private String message;
14 @ApiModelProperty(value = "结果")
15 private T payload;
16
17 private Result<T> code(int code) {
18 this.code = code;
19 return this;
20 }
21
22 private Result<T> message(String message) {
23 this.message = message;
24 return this;
25 }
26
27 private Result<T> payload(T payload) {
28 this.payload = payload;
29 return this;
30 }
31
32 public static <T> Result<T> ok() {
33 return new Result<T>().code(ResultCode.SUCCESS.getCode()).message(ResultCode.SUCCESS.getMessage()).payload(null);
34 }
35
36 public static <T> Result<T> ok(T payload) {
37 return new Result<T>().code(ResultCode.SUCCESS.getCode()).message(ResultCode.SUCCESS.getMessage()).payload(payload);
38 }
39
40 public static <T> Result<T> fail() {
41 return new Result<T>().code(ResultCode.FAIL.getCode()).message(ResultCode.FAIL.getMessage()).payload(null);
42 }
43
44 public static <T> Result<T> result(int code, String message, T payload) {
45 return new Result<T>().code(code).message(message).payload(payload);
46 }
47}
48
49
50
状态码
1@Getter
2public enum ResultCode {
3 //成功
4 SUCCESS(20000, "成功"),
5 //失败
6 FAIL(40000, "失败"),
7 //未认证(签名错误)
8 UNAUTHORIZED(40001, "未认证(签名错误)"),
9 //接口不存在
10 NOT_FOUND(40004, "接口不存在"),
11 //服务器内部错误
12 INTERNAL_SERVER_ERROR(50000, "服务器内部错误"),
13 //TOKEN已过期
14 TOKEN_INVAILD(10001, "TOKEN已过期"),
15 //TOKEN无效
16 TOKEN_NOTFOUND(10002, "TOKEN无效");
17 private final int code;
18 private final String message;
19
20 ResultCode(int code, String message) {
21 this.code = code;
22 this.message = message;
23 }
24}
25
26
Controller 层使用
1 @GetMapping(value = "/detail")
2 @ApiOperation(value = "根据id查询用户")
3 public Result<User> detail(@RequestParam("userId") Long userId) {
4 log.info("根据userId查询用户信息【{}】", userId);
5 User user = userService.findByUserId(userId);
6 log.info("用户信息【{}】", user.toString());
7 return Result.ok(user);
8 }
PageInfo 封装
用于 PageHelper显示属性值
1
2import com.github.pagehelper.Page;
3import io.swagger.annotations.ApiModelProperty;
4import lombok.Data;
5
6import java.util.Collection;
7import java.util.List;
8
9/**
10 * @author felix
11 * @ 日期 2019-06-01 15:57
12 */
13@Data
14public class MyPageInfo<T> {
15 @ApiModelProperty(value = "当前页")
16 private int pageNum;
17 @ApiModelProperty(value = "每页的数量")
18 private int pageSize;
19 @ApiModelProperty(value = "当前页的数量")
20 private int size;
21 /**
22 * 由于startRow和endRow不常用,这里说个具体的用法
23 * 可以在页面中"显示startRow到endRow 共size条数据"
24 */
25 @ApiModelProperty(value = "当前页面第一个元素在数据库中的行号")
26 private int startRow;
27 @ApiModelProperty(value = "当前页面最后一个元素在数据库中的行号")
28 private int endRow;
29 @ApiModelProperty(value = "总页数")
30 private int pages;
31 @ApiModelProperty(value = "前一页")
32 private int prePage;
33 @ApiModelProperty(value = "下一页")
34 private int nextPage;
35 @ApiModelProperty(value = "是否为第一页")
36 private boolean firstPage = false;
37 @ApiModelProperty(value = "是否为最后一页")
38 private boolean lastPage = false;
39 @ApiModelProperty(value = "是否有前一页")
40 private boolean hasPreviousPage = false;
41 @ApiModelProperty(value = "是否有下一页")
42 private boolean hasNextPage = false;
43 @ApiModelProperty(value = "导航页码数")
44 private int navigatePages;
45 @ApiModelProperty(value = "所有导航页号")
46 private int[] navigatepageNums;
47 @ApiModelProperty(value = "导航条上的第一页")
48 private int navigateFirstPage;
49 @ApiModelProperty(value = "导航条上的最后一页")
50 private int navigateLastPage;
51 @ApiModelProperty(value = "总页数")
52 private long total;
53 @ApiModelProperty(value = "结果集")
54 private List<T> list;
55
56
57 public MyPageInfo() {
58 this.firstPage = false;
59 this.lastPage = false;
60 this.hasPreviousPage = false;
61 this.hasNextPage = false;
62 }
63
64 public MyPageInfo(List<T> list) {
65 this(list, 8);
66 this.list = list;
67 if (list instanceof Page) {
68 this.total = ((Page) list).getTotal();
69 } else {
70 this.total = (long) list.size();
71 }
72 }
73
74 public MyPageInfo(List<T> list, int navigatePages) {
75 this.firstPage = false;
76 this.lastPage = false;
77 this.hasPreviousPage = false;
78 this.hasNextPage = false;
79 if (list instanceof Page) {
80 Page page = (Page) list;
81 this.pageNum = page.getPageNum();
82 this.pageSize = page.getPageSize();
83 this.pages = page.getPages();
84 this.size = page.size();
85 if (this.size == 0) {
86 this.startRow = 0;
87 this.endRow = 0;
88 } else {
89 this.startRow = page.getStartRow() + 1;
90 this.endRow = this.startRow - 1 + this.size;
91 }
92 } else if (list instanceof Collection) {
93 this.pageNum = 1;
94 this.pageSize = list.size();
95 this.pages = this.pageSize > 0 ? 1 : 0;
96 this.size = list.size();
97 this.startRow = 0;
98 this.endRow = list.size() > 0 ? list.size() - 1 : 0;
99 }
100
101 if (list instanceof Collection) {
102 this.navigatePages = navigatePages;
103 this.calcNavigatepageNums();
104 this.calcPage();
105 this.judgePageBoudary();
106 }
107
108 }
109
110 private void calcNavigatepageNums() {
111 //当总页数小于或等于导航页码数时
112 if (pages <= navigatePages) {
113 navigatepageNums = new int[pages];
114 for (int i = 0; i < pages; i++) {
115 navigatepageNums[i] = i + 1;
116 }
117 } else { //当总页数大于导航页码数时
118 navigatepageNums = new int[navigatePages];
119 int startNum = pageNum - navigatePages / 2;
120 int endNum = pageNum + navigatePages / 2;
121
122 if (startNum < 1) {
123 startNum = 1;
124 //(最前navigatePages页
125 for (int i = 0; i < navigatePages; i++) {
126 navigatepageNums[i] = startNum++;
127 }
128 } else if (endNum > pages) {
129 endNum = pages;
130 //最后navigatePages页
131 for (int i = navigatePages - 1; i >= 0; i--) {
132 navigatepageNums[i] = endNum--;
133 }
134 } else {
135 //所有中间页
136 for (int i = 0; i < navigatePages; i++) {
137 navigatepageNums[i] = startNum++;
138 }
139 }
140 }
141 }
142
143 private void calcPage() {
144 if (this.navigatepageNums != null && this.navigatepageNums.length > 0) {
145 this.navigateFirstPage = this.navigatepageNums[0];
146 this.navigateLastPage = this.navigatepageNums[this.navigatepageNums.length - 1];
147 if (this.pageNum > 1) {
148 this.prePage = this.pageNum - 1;
149 }
150
151 if (this.pageNum < this.pages) {
152 this.nextPage = this.pageNum + 1;
153 }
154 }
155
156 }
157
158 private void judgePageBoudary() {
159 this.firstPage = this.pageNum == 1;
160 this.lastPage = this.pageNum == this.pages || this.pages == 0;
161 this.hasPreviousPage = this.pageNum > 1;
162 this.hasNextPage = this.pageNum < this.pages;
163 }
164}
对pagehelp的使用方式
1 @GetMapping(value = "/list")
2 @ApiOperation(value = "获取用户列表")
3 public ResponseData<MyPageInfo<User>> getList(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
4 PageHelper.startPage(page, size);
5 List<User> list = userService.getList();
6 log.info("查询用户列表记录数为 :【{}】", list.size());
7 MyPageInfo<User> pageInfo = new MyPageInfo<>(list);
8 return ResultGenerator.successResult(pageInfo);
9 }