-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleApplication.java
More file actions
113 lines (101 loc) · 3.79 KB
/
Copy pathSampleApplication.java
File metadata and controls
113 lines (101 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package io.github.luozhan.excel.sample;
import io.github.luozhan.excel.converter.BooleanToChineseConverter;
import io.github.luozhan.excel.paramhandle.req.PageParamHandler;
import io.github.luozhan.excel.paramhandle.rsp.ExcelDataConverter;
import io.github.luozhan.excel.sample.model.MyPage;
import io.github.luozhan.excel.sample.model.MyPageRequest;
import io.github.luozhan.excel.sample.model.Result;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Page;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootApplication
@MapperScan("io.github.luozhan.excel.integration")
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
/**
* 注册布尔转换器
*/
@Bean
public BooleanToChineseConverter booleanToChineseConverter() {
return new BooleanToChineseConverter();
}
/**
* 统一响应体 -> List的转换器
* 声明如何从Result中拿到excel数据集
*/
@Bean
@SuppressWarnings("Convert2Lambda")
public ExcelDataConverter<Result<?>> resultToListConverter() {
return new ExcelDataConverter<Result<?>>() {
@Override
public List<?> convert(@NonNull Result<?> source) {
Object data = source.getData();
if (data instanceof List) {
return (List<?>) data;
}
if (data instanceof MyPage) {
return ((Page<?>) data).getContent();
}
return Collections.singletonList(data);
}
};
}
/**
* 注册自定义分页参数处理器
* 声明如何设置分页参数
*/
@Bean
public PageParamHandler<MyPageRequest> myPageParamHandler() {
// 匿名内部类,入参是页码设置函数和一页条数设置函数
return new PageParamHandler<MyPageRequest>() {
@Override
public void accept(MyPageRequest page, int pageNumber, int pageSize) {
page.setPageNum(pageNumber);
page.setPageSize(pageSize);
}
};
}
/**
* Page -> List转换器
* 声明如何从分页结果中拿到excel数据集
*/
@Bean
@SuppressWarnings("Convert2Lambda")
public ExcelDataConverter<MyPage<?>> pageToListConverter() {
return new ExcelDataConverter<MyPage<?>>() {
@Override
public List<?> convert(@NonNull MyPage<?> source) {
return source.getContent();
}
};
}
@RestControllerAdvice
public static class GlobalExceptionHandler {
/**
* 捕获 运行时异常(通用异常)
*/
@ExceptionHandler(RuntimeException.class)
public Map<String, Object> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
return getErrorMap(500, "服务器异常:" + e.getMessage(), request.getRequestURI());
}
private Map<String, Object> getErrorMap(int code, String msg, String path) {
Map<String, Object> result = new HashMap<>();
result.put("code", code); // 状态码
result.put("msg", msg); // 错误提示
result.put("path", path); // 请求接口路径
return result;
}
}
}