24
24
import org .springframework .beans .BeansException ;
25
25
import org .springframework .boot .SpringApplication ;
26
26
import org .springframework .boot .autoconfigure .SpringBootApplication ;
27
- import org .springframework .boot .context . embedded . ConfigurableEmbeddedServletContainer ;
28
- import org .springframework .boot .context . embedded . EmbeddedServletContainerCustomizer ;
27
+ import org .springframework .boot .web . server . WebServerFactoryCustomizer ;
28
+ import org .springframework .boot .web . servlet . server . ConfigurableServletWebServerFactory ;
29
29
import org .springframework .context .ApplicationContext ;
30
30
import org .springframework .context .ApplicationContextAware ;
31
31
import org .springframework .context .annotation .Bean ;
32
32
import org .springframework .context .annotation .Configuration ;
33
- import org .springframework .web .cors .CorsConfiguration ;
34
- import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
35
- import org .springframework .web .filter .CorsFilter ;
33
+ import org .springframework .web .servlet .config .annotation .CorsRegistry ;
34
+ import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
36
35
37
36
import com .alibaba .fastjson .JSON ;
38
37
import com .alibaba .fastjson .JSONObject ;
39
38
import com .alibaba .fastjson .serializer .PropertyFilter ;
40
39
41
40
import apijson .Log ;
41
+ import apijson .NotNull ;
42
42
import apijson .StringUtil ;
43
43
import apijson .demo .DemoFunctionParser ;
44
44
import apijson .demo .DemoParser ;
57
57
import unitauto .MethodUtil .Argument ;
58
58
import unitauto .MethodUtil .InstanceGetter ;
59
59
import unitauto .MethodUtil .JSONCallback ;
60
- import unitauto .NotNull ;
61
60
import unitauto .jar .UnitAutoApp ;
62
61
63
62
67
66
*/
68
67
@ Configuration
69
68
@ SpringBootApplication
70
- public class DemoApplication implements ApplicationContextAware {
69
+ public class DemoApplication implements ApplicationContextAware , WebServerFactoryCustomizer < ConfigurableServletWebServerFactory > {
71
70
private static final String TAG = "DemoApplication" ;
72
71
73
72
static {
74
73
// APIJSON 配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
75
-
74
+
76
75
Map <String , Pattern > COMPILE_MAP = AbstractVerifier .COMPILE_MAP ;
77
76
COMPILE_MAP .put ("PHONE" , StringUtil .PATTERN_PHONE );
78
77
COMPILE_MAP .put ("EMAIL" , StringUtil .PATTERN_EMAIL );
79
78
COMPILE_MAP .put ("ID_CARD" , StringUtil .PATTERN_ID_CARD );
80
-
79
+
81
80
// 使用本项目的自定义处理类
82
81
APIJSONApplication .DEFAULT_APIJSON_CREATOR = new APIJSONCreator () {
83
-
82
+
84
83
@ Override
85
84
public Parser <Long > createParser () {
86
85
return new DemoParser ();
@@ -89,12 +88,12 @@ public Parser<Long> createParser() {
89
88
public FunctionParser createFunctionParser () {
90
89
return new DemoFunctionParser ();
91
90
}
92
-
91
+
93
92
@ Override
94
93
public Verifier <Long > createVerifier () {
95
94
return new DemoVerifier ();
96
95
}
97
-
96
+
98
97
@ Override
99
98
public SQLConfig createSQLConfig () {
100
99
return new DemoSQLConfig ();
@@ -103,17 +102,17 @@ public SQLConfig createSQLConfig() {
103
102
public SQLExecutor createSQLExecutor () {
104
103
return new DemoSQLExecutor ();
105
104
}
106
-
105
+
107
106
};
108
-
107
+
109
108
// APIJSON 配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
110
-
111
-
112
-
109
+
110
+
111
+
113
112
// UnitAuto 单元测试配置 https://github.com/TommyLemon/UnitAuto <<<<<<<<<<<<<<<<<<<<<<<<<<<
114
-
113
+
115
114
UnitAutoApp .init ();
116
-
115
+
117
116
// 适配 Spring 注入的类及 Context 等环境相关的类
118
117
final InstanceGetter ig = MethodUtil .INSTANCE_GETTER ;
119
118
MethodUtil .INSTANCE_GETTER = new InstanceGetter () {
@@ -131,7 +130,7 @@ public Object getInstance(@NotNull Class<?> clazz, List<Argument> classArgs, Boo
131
130
return ig .getInstance (clazz , classArgs , reuse );
132
131
}
133
132
};
134
-
133
+
135
134
// 排除转换 JSON 异常的类,一般是 Context 等环境相关的类
136
135
final JSONCallback jc = MethodUtil .JSON_CALLBACK ;
137
136
MethodUtil .JSON_CALLBACK = new JSONCallback () {
@@ -145,7 +144,7 @@ public JSONObject newSuccessResult() {
145
144
public JSONObject newErrorResult (Throwable e ) {
146
145
return jc .newErrorResult (e );
147
146
}
148
-
147
+
149
148
@ Override
150
149
public JSONObject parseJSON (String type , Object value ) {
151
150
if (value == null || unitauto .JSON .isBooleanOrNumberOrString (value ) || value instanceof JSON || value instanceof Enum ) {
@@ -154,7 +153,6 @@ public JSONObject parseJSON(String type, Object value) {
154
153
155
154
if (value instanceof ApplicationContext
156
155
|| value instanceof Context
157
- || value instanceof javax .validation .MessageInterpolator .Context
158
156
|| value instanceof org .omg .CORBA .Context
159
157
|| value instanceof org .apache .catalina .Context
160
158
|| value instanceof ch .qos .logback .core .Context
@@ -172,7 +170,6 @@ public boolean apply(Object object, String name, Object value) {
172
170
173
171
if (value instanceof ApplicationContext
174
172
|| value instanceof Context
175
- || value instanceof javax .validation .MessageInterpolator .Context
176
173
|| value instanceof org .omg .CORBA .Context
177
174
|| value instanceof org .apache .catalina .Context
178
175
|| value instanceof ch .qos .logback .core .Context
@@ -190,12 +187,12 @@ public boolean apply(Object object, String name, Object value) {
190
187
191
188
return jc .parseJSON (type , value );
192
189
}
193
-
190
+
194
191
};
195
-
192
+
196
193
// UnitAuto 单元测试配置 https://github.com/TommyLemon/UnitAuto >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
197
-
198
-
194
+
195
+
199
196
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
200
197
// try { //加载驱动程序
201
198
// Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
@@ -228,66 +225,47 @@ public boolean apply(Object object, String name, Object value) {
228
225
// }
229
226
}
230
227
231
-
228
+
232
229
public static void main (String [] args ) throws Exception {
233
230
SpringApplication .run (DemoApplication .class , args );
234
231
235
232
Log .DEBUG = true ; // 上线生产环境前改为 false,可不输出 APIJSONORM 的日志 以及 SQLException 的原始(敏感)信息
236
233
APIJSONApplication .init ();
237
234
}
238
-
239
-
235
+
236
+ // SpringBoot 2.x 自定义端口方式
237
+ @ Override
238
+ public void customize (ConfigurableServletWebServerFactory server ) {
239
+ server .setPort (8080 );
240
+ }
241
+
242
+ // 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例
240
243
private static ApplicationContext APPLICATION_CONTEXT ;
241
244
public static ApplicationContext getApplicationContext () {
242
245
return APPLICATION_CONTEXT ;
243
246
}
244
247
@ Override
245
248
public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
246
249
APPLICATION_CONTEXT = applicationContext ;
247
- }
248
-
249
- // SpringBoot 2.x 自定义端口方式
250
- // @Bean
251
- // public TomcatServletWebServerFactory servletContainer(){
252
- // return new TomcatServletWebServerFactory(8081) ;
253
- // }
254
- // SpringBoot 1.x 自定义端口方式,配置文件加 server.port=80 无效(MacOS 10.10.?)
255
- @ Bean
256
- public EmbeddedServletContainerCustomizer containerCustomizer () {
257
- return new EmbeddedServletContainerCustomizer () {
250
+ }
251
+
252
+
253
+ // 支持 APIAuto 中 JavaScript 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
258
254
255
+ @ Bean
256
+ public WebMvcConfigurer corsConfigurer () {
257
+ return new WebMvcConfigurer () {
259
258
@ Override
260
- public void customize (ConfigurableEmbeddedServletContainer container ) {
261
- container .setPort (8080 ); //自定义端口号,如果和 TiDB 等其它程序端口有冲突,可改为 8081, 9090, 9091 等未被占用的端口
259
+ public void addCorsMappings (CorsRegistry registry ) {
260
+ registry .addMapping ("/**" )
261
+ .allowedOriginPatterns ("*" )
262
+ .allowedMethods ("*" )
263
+ .allowCredentials (true )
264
+ .maxAge (3600 );
262
265
}
263
266
};
264
267
}
265
268
266
-
267
- // 支持 APIAuto 中 JavaScript 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
268
-
269
- /**
270
- * 跨域过滤器
271
- * @return
272
- */
273
- @ Bean
274
- public CorsFilter corsFilter () {
275
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
276
- source .registerCorsConfiguration ("/**" , buildConfig ());
277
- return new CorsFilter (source );
278
- }
279
- /**CORS跨域配置
280
- * @return
281
- */
282
- private CorsConfiguration buildConfig () {
283
- CorsConfiguration corsConfiguration = new CorsConfiguration ();
284
- corsConfiguration .addAllowedOrigin ("*" ); //允许的域名或IP地址
285
- corsConfiguration .addAllowedHeader ("*" ); //允许的请求头
286
- corsConfiguration .addAllowedMethod ("*" ); //允许的HTTP请求方法
287
- corsConfiguration .setAllowCredentials (true ); //允许发送跨域凭据,前端Axios存取JSESSIONID必须要
288
- return corsConfiguration ;
289
- }
290
-
291
269
// 支持 APIAuto 中 JavaScript 代码跨域请求 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
292
270
293
271
}
0 commit comments