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 4749bda

Browse files
Java:APIJSONDemo 新增支持 CORS 跨域;3 个 Demo 都升级 APIJSON 和 apijson-framework 版本至最新的 4.5.1;APIJSONBoot 适配 SpringBoot 2.4.2; APIJSONBoot 解决打包后单元测试因缺少 classgraph 依赖导致导入用例等功能不能正常使用;
1 parent 378c101 commit 4749bda

File tree

10 files changed

+89
-82
lines changed

10 files changed

+89
-82
lines changed
63.7 KB
Binary file not shown.
280 KB
Binary file not shown.

‎APIJSON-Java-Server/APIJSONBoot/pom.xml‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
<artifactId>javax.servlet-api</artifactId>
3232
<version>4.0.1</version>
3333
</dependency>
34-
35-
<!-- 单元测试:可使用 libs 目录的 unitauto-java.jar 和 unitauto-jar.jar 来替代,两种方式二选一 <<<<<<<<< -->
34+
35+
<!-- 单元测试:可使用 libs 目录的 unitauto-java.jar 和 unitauto-jar.jar 来替代,两种方式二选一
36+
<<<<<<<<< -->
3637
<dependency>
3738
<groupId>com.github.TommyLemon</groupId>
3839
<artifactId>unitauto-java</artifactId>
@@ -43,19 +44,25 @@
4344
<artifactId>unitauto-jar</artifactId>
4445
<version>2.5.3</version>
4546
</dependency>
46-
<!-- 单元测试:可使用 libs 目录的 unitauto-java.jar 和 unitauto-jar.jar 来替代,两种方式二选一 >>>>>>>>> -->
47-
47+
<dependency>
48+
<groupId>io.github.classgraph</groupId>
49+
<artifactId>classgraph</artifactId>
50+
<version>4.8.87</version>
51+
</dependency>
52+
<!-- 单元测试:可使用 libs 目录的 unitauto-java.jar 和 unitauto-jar.jar 来替代,两种方式二选一
53+
>>>>>>>>> -->
54+
4855

4956
<!-- 可使用 libs 目录的 apijson-orm.jar 和 apijson-framework.jar 来替代,两种方式二选一 <<<<<<<<<< -->
5057
<dependency>
5158
<groupId>com.github.Tencent</groupId>
5259
<artifactId>APIJSON</artifactId>
53-
<version>4.4.8</version>
60+
<version>4.5.1</version>
5461
</dependency>
5562
<dependency>
5663
<groupId>com.github.APIJSON</groupId>
5764
<artifactId>apijson-framework</artifactId>
58-
<version>4.5.0</version>
65+
<version>4.5.1</version>
5966
</dependency>
6067
<!-- 可使用 libs 目录的 apijson-orm.jar 和 apijson-framework.jar 来替代,两种方式二选一 >>>>>>>>>> -->
6168

@@ -77,9 +84,9 @@
7784
</dependency>
7885
<!-- 没找到合适且稳定的 Oracle 的 JDBC Maven 依赖,需要自行下载 jar 包 -->
7986
<!-- 数据库 JDBC 驱动 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
80-
8187

82-
<!-- APIJSONBoot 需要用的 SpringBoot 框架,1.4.1 以上 -->
88+
89+
<!-- APIJSONBoot 需要用的 SpringBoot 框架,1.4.0 以上 -->
8390
<dependency>
8491
<groupId>org.springframework.boot</groupId>
8592
<artifactId>spring-boot-starter-web</artifactId>

‎APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoApplication.java‎

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424
import org.springframework.beans.BeansException;
2525
import org.springframework.boot.SpringApplication;
2626
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;
2929
import org.springframework.context.ApplicationContext;
3030
import org.springframework.context.ApplicationContextAware;
3131
import org.springframework.context.annotation.Bean;
3232
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;
3635

3736
import com.alibaba.fastjson.JSON;
3837
import com.alibaba.fastjson.JSONObject;
3938
import com.alibaba.fastjson.serializer.PropertyFilter;
4039

4140
import apijson.Log;
41+
import apijson.NotNull;
4242
import apijson.StringUtil;
4343
import apijson.demo.DemoFunctionParser;
4444
import apijson.demo.DemoParser;
@@ -57,7 +57,6 @@
5757
import unitauto.MethodUtil.Argument;
5858
import unitauto.MethodUtil.InstanceGetter;
5959
import unitauto.MethodUtil.JSONCallback;
60-
import unitauto.NotNull;
6160
import unitauto.jar.UnitAutoApp;
6261

6362

@@ -67,20 +66,20 @@
6766
*/
6867
@Configuration
6968
@SpringBootApplication
70-
public class DemoApplication implements ApplicationContextAware {
69+
public class DemoApplication implements ApplicationContextAware, WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
7170
private static final String TAG = "DemoApplication";
7271

7372
static {
7473
// APIJSON 配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
75-
74+
7675
Map<String, Pattern> COMPILE_MAP = AbstractVerifier.COMPILE_MAP;
7776
COMPILE_MAP.put("PHONE", StringUtil.PATTERN_PHONE);
7877
COMPILE_MAP.put("EMAIL", StringUtil.PATTERN_EMAIL);
7978
COMPILE_MAP.put("ID_CARD", StringUtil.PATTERN_ID_CARD);
80-
79+
8180
// 使用本项目的自定义处理类
8281
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator() {
83-
82+
8483
@Override
8584
public Parser<Long> createParser() {
8685
return new DemoParser();
@@ -89,12 +88,12 @@ public Parser<Long> createParser() {
8988
public FunctionParser createFunctionParser() {
9089
return new DemoFunctionParser();
9190
}
92-
91+
9392
@Override
9493
public Verifier<Long> createVerifier() {
9594
return new DemoVerifier();
9695
}
97-
96+
9897
@Override
9998
public SQLConfig createSQLConfig() {
10099
return new DemoSQLConfig();
@@ -103,17 +102,17 @@ public SQLConfig createSQLConfig() {
103102
public SQLExecutor createSQLExecutor() {
104103
return new DemoSQLExecutor();
105104
}
106-
105+
107106
};
108-
107+
109108
// APIJSON 配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
110-
111-
112-
109+
110+
111+
113112
// UnitAuto 单元测试配置 https://github.com/TommyLemon/UnitAuto <<<<<<<<<<<<<<<<<<<<<<<<<<<
114-
113+
115114
UnitAutoApp.init();
116-
115+
117116
// 适配 Spring 注入的类及 Context 等环境相关的类
118117
final InstanceGetter ig = MethodUtil.INSTANCE_GETTER;
119118
MethodUtil.INSTANCE_GETTER = new InstanceGetter() {
@@ -131,7 +130,7 @@ public Object getInstance(@NotNull Class<?> clazz, List<Argument> classArgs, Boo
131130
return ig.getInstance(clazz, classArgs, reuse);
132131
}
133132
};
134-
133+
135134
// 排除转换 JSON 异常的类,一般是 Context 等环境相关的类
136135
final JSONCallback jc = MethodUtil.JSON_CALLBACK;
137136
MethodUtil.JSON_CALLBACK = new JSONCallback() {
@@ -145,7 +144,7 @@ public JSONObject newSuccessResult() {
145144
public JSONObject newErrorResult(Throwable e) {
146145
return jc.newErrorResult(e);
147146
}
148-
147+
149148
@Override
150149
public JSONObject parseJSON(String type, Object value) {
151150
if (value == null || unitauto.JSON.isBooleanOrNumberOrString(value) || value instanceof JSON || value instanceof Enum) {
@@ -154,7 +153,6 @@ public JSONObject parseJSON(String type, Object value) {
154153

155154
if (value instanceof ApplicationContext
156155
|| value instanceof Context
157-
|| value instanceof javax.validation.MessageInterpolator.Context
158156
|| value instanceof org.omg.CORBA.Context
159157
|| value instanceof org.apache.catalina.Context
160158
|| value instanceof ch.qos.logback.core.Context
@@ -172,7 +170,6 @@ public boolean apply(Object object, String name, Object value) {
172170

173171
if (value instanceof ApplicationContext
174172
|| value instanceof Context
175-
|| value instanceof javax.validation.MessageInterpolator.Context
176173
|| value instanceof org.omg.CORBA.Context
177174
|| value instanceof org.apache.catalina.Context
178175
|| value instanceof ch.qos.logback.core.Context
@@ -190,12 +187,12 @@ public boolean apply(Object object, String name, Object value) {
190187

191188
return jc.parseJSON(type, value);
192189
}
193-
190+
194191
};
195-
192+
196193
// UnitAuto 单元测试配置 https://github.com/TommyLemon/UnitAuto >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
197-
198-
194+
195+
199196
// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
200197
// try { //加载驱动程序
201198
// Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
@@ -228,66 +225,47 @@ public boolean apply(Object object, String name, Object value) {
228225
// }
229226
}
230227

231-
228+
232229
public static void main(String[] args) throws Exception {
233230
SpringApplication.run(DemoApplication.class, args);
234231

235232
Log.DEBUG = true; // 上线生产环境前改为 false,可不输出 APIJSONORM 的日志 以及 SQLException 的原始(敏感)信息
236233
APIJSONApplication.init();
237234
}
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 注入的类实例
240243
private static ApplicationContext APPLICATION_CONTEXT;
241244
public static ApplicationContext getApplicationContext() {
242245
return APPLICATION_CONTEXT;
243246
}
244247
@Override
245248
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
246249
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 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
258254

255+
@Bean
256+
public WebMvcConfigurer corsConfigurer() {
257+
return new WebMvcConfigurer() {
259258
@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);
262265
}
263266
};
264267
}
265268

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-
291269
// 支持 APIAuto 中 JavaScript 代码跨域请求 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
292270

293271
}

‎APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoObjectParser.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
import java.util.List;
1818

1919
import javax.servlet.http.HttpSession;
20-
import javax.validation.constraints.NotNull;
2120

2221
import com.alibaba.fastjson.JSONObject;
2322

23+
import apijson.NotNull;
2424
import apijson.RequestMethod;
2525
import apijson.orm.AbstractObjectParser;
2626
import apijson.orm.AbstractParser;

‎APIJSON-Java-Server/APIJSONDemo/pom.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
<properties>
1515
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16-
<project.reporting.outputEncoding>UTF-8
17-
</project.reporting.outputEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1817
<java.version>1.8</java.version>
1918
</properties>
2019

@@ -32,13 +31,14 @@
3231
</dependency>
3332
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
3433

34+
<!-- APIJSONBoot 需要的 APIJSON 相关依赖 -->
3535
<dependency>
3636
<groupId>com.github.APIJSON</groupId>
3737
<artifactId>apijson-framework</artifactId>
3838
<version>LATEST</version>
3939
</dependency>
4040

41-
<!-- APIJSONBoot 需要用的 SpringBoot 框架,1.4.1 以上 -->
41+
<!-- APIJSONBoot 需要用的 SpringBoot 框架,1.4.0 以上 -->
4242
<dependency>
4343
<groupId>org.springframework.boot</groupId>
4444
<artifactId>spring-boot-starter-web</artifactId>

‎APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/DemoApplication.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
import org.springframework.boot.SpringApplication;
1818
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
import org.springframework.context.annotation.Bean;
1920
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
22+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2023

2124
import apijson.framework.APIJSONApplication;
2225
import apijson.framework.APIJSONCreator;
@@ -66,5 +69,24 @@ public static void main(String[] args) throws Exception {
6669
SpringApplication.run(DemoApplication.class, args);
6770
APIJSONApplication.init(false); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
6871
}
72+
73+
74+
// 支持 APIAuto 中 JavaScript 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
75+
76+
@Bean
77+
public WebMvcConfigurer corsConfigurer() {
78+
return new WebMvcConfigurer() {
79+
@Override
80+
public void addCorsMappings(CorsRegistry registry) {
81+
registry.addMapping("/**")
82+
.allowedOriginPatterns("*")
83+
.allowedMethods("*")
84+
.allowCredentials(true)
85+
.maxAge(3600);
86+
}
87+
};
88+
}
89+
90+
// 支持 APIAuto 中 JavaScript 代码跨域请求 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
6991

7092
}
63.7 KB
Binary file not shown.
280 KB
Binary file not shown.

‎APIJSON-Java-Server/APIJSONFinal/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
<dependency>
2525
<groupId>com.github.Tencent</groupId>
2626
<artifactId>APIJSON</artifactId>
27-
<version>4.4.8</version>
27+
<version>4.5.1</version>
2828
</dependency>
2929
<dependency>
3030
<groupId>com.github.APIJSON</groupId>
3131
<artifactId>apijson-framework</artifactId>
32-
<version>4.5.0</version>
32+
<version>4.5.1</version>
3333
</dependency>
3434
<!-- 可使用 libs 目录的 apijson-orm.jar 和 apijson-framework.jar 来替代,两种方式二选一 >>>>>>>>>> -->
3535

0 commit comments

Comments
(0)

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