Skip to content

Commit 8947fa3

Browse files
authored
feat: Add JsonUtils and modify related methods (opentiny#243)
1 parent 773dfd8 commit 8947fa3

28 files changed

+765
-212
lines changed

app/src/main/java/com/tinyengine/it/config/filter/FilterConfig.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import com.baomidou.mybatisplus.annotation.DbType;
1616
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
1717
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
18-
import com.fasterxml.jackson.databind.ObjectMapper;
19-
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
2018

2119
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2220
import org.springframework.context.annotation.Bean;
@@ -56,16 +54,4 @@ public FilterRegistrationBean<RequestIdFilter> requestIdFilter() {
5654
registrationBean.setFilter(new RequestIdFilter());
5755
return registrationBean;
5856
}
59-
60-
/**
61-
* Object mapper object mapper.
62-
*
63-
* @return the object mapper
64-
*/
65-
@Bean
66-
public ObjectMapper objectMapper() {
67-
ObjectMapper mapper = new ObjectMapper();
68-
mapper.registerModule(new JavaTimeModule());
69-
return mapper;
70-
}
7157
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/**
22
* Copyright (c) 2023 - present TinyEngine Authors.
33
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4-
*
4+
* <p>
55
* Use of this source code is governed by an MIT-style license.
6-
*
6+
* <p>
77
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
88
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
99
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10-
*
1110
*/
1211

1312
package com.tinyengine.it.config.filter;
@@ -16,31 +15,35 @@
1615
import org.springframework.context.annotation.Bean;
1716
import org.springframework.context.annotation.Configuration;
1817
import org.springframework.web.cors.CorsConfiguration;
19-
import org.springframework.web.cors.reactive.CorsWebFilter;
20-
import org.springframework.web.reactive.config.WebFluxConfigurer;
18+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
19+
import org.springframework.web.filter.CorsFilter;
20+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2121

2222
import java.util.Arrays;
2323
import java.util.List;
2424

2525
@Configuration
26-
public class WebConfig implements WebFluxConfigurer {
26+
public class WebConfig implements WebMvcConfigurer {
2727
@Value("${cors.allowed-origins}")
2828
private String allowedOrigins;
2929

3030
@Bean
31-
public CorsWebFilter corsFilter() {
31+
public CorsFilter corsFilter() {
32+
// 跨域配置地址
3233
List<String> crosDomainList = Arrays.asList(allowedOrigins.split(","));
3334

3435
CorsConfiguration corsConfiguration = new CorsConfiguration();
36+
// 1、允许来源
3537
corsConfiguration.setAllowedOriginPatterns(crosDomainList);
38+
// 2、允许任何请求头
3639
corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
40+
// 3、允许任何方法
3741
corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
42+
// 4、允许凭证
3843
corsConfiguration.setAllowCredentials(true);
3944

40-
org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource source =
41-
new org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource();
45+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
4246
source.registerCorsConfiguration("/**", corsConfiguration);
43-
44-
return new CorsWebFilter(source);
47+
return new CorsFilter(source);
4548
}
46-
}
49+
}

base/src/main/java/com/tinyengine/it/common/handler/ListTypeHandler.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import com.fasterxml.jackson.core.JsonProcessingException;
1616
import com.fasterxml.jackson.core.type.TypeReference;
17-
import com.fasterxml.jackson.databind.ObjectMapper;
1817

18+
import com.tinyengine.it.common.utils.JsonUtils;
1919
import lombok.extern.slf4j.Slf4j;
2020

2121
import org.apache.ibatis.type.BaseTypeHandler;
@@ -38,14 +38,12 @@
3838
*/
3939
@Slf4j
4040
public class ListTypeHandler extends BaseTypeHandler<List<?>> {
41-
private final ObjectMapper objectMapper = new ObjectMapper();
42-
4341
@Override
4442
public void setNonNullParameter(PreparedStatement ps, int i, List<?> parameter, JdbcType jdbcType)
4543
throws SQLException {
4644
// 将 List<?> 转换为字符串,并设置到 PreparedStatement 中的相应参数
4745
try {
48-
String json = objectMapper.writeValueAsString(parameter);
46+
String json = JsonUtils.MAPPER.writeValueAsString(parameter);
4947
ps.setString(i, json);
5048
} catch (IOException e) {
5149
throw new SQLException("Error converting List<?> to JSON", e);
@@ -90,10 +88,10 @@ private List<?> convetJsonList(String jsonString) throws JsonProcessingException
9088
return Collections.emptyList();
9189
} else if (jsonString.startsWith("[{") && jsonString.endsWith("}]")) {
9290
// 尝试将 JSON 字符串转换为 List<Map<String, Object>>
93-
return objectMapper.readValue(jsonString, new TypeReference<List<Map<String, Object>>>() {});
91+
return JsonUtils.MAPPER.readValue(jsonString, new TypeReference<List<Map<String, Object>>>() {});
9492
} else {
9593
// 尝试将 JSON 字符串转换为 List<String>
96-
return objectMapper.readValue(jsonString, new TypeReference<List<String>>() {});
94+
return JsonUtils.MAPPER.readValue(jsonString, new TypeReference<List<String>>() {});
9795
}
9896
}
9997
}

base/src/main/java/com/tinyengine/it/common/handler/MapTypeHandler.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import com.fasterxml.jackson.core.JsonProcessingException;
1616
import com.fasterxml.jackson.core.type.TypeReference;
1717
import com.fasterxml.jackson.databind.JsonNode;
18-
import com.fasterxml.jackson.databind.ObjectMapper;
1918

19+
import com.tinyengine.it.common.utils.JsonUtils;
2020
import lombok.extern.slf4j.Slf4j;
2121

2222
import org.apache.ibatis.type.BaseTypeHandler;
@@ -37,13 +37,11 @@
3737
*/
3838
@Slf4j
3939
public class MapTypeHandler extends BaseTypeHandler<Map<String, Object>> {
40-
private final ObjectMapper objectMapper = new ObjectMapper();
41-
4240
@Override
4341
public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType)
4442
throws SQLException {
4543
try {
46-
String json = objectMapper.writeValueAsString(parameter);
44+
String json = JsonUtils.MAPPER.writeValueAsString(parameter);
4745
ps.setString(i, json);
4846
} catch (JsonProcessingException e) {
4947
throw new SQLException("Error converting Map to JSON", e);
@@ -71,9 +69,9 @@ private Map<String, Object> parseJson(String json) throws SQLException {
7169
return new HashMap<>();
7270
}
7371
try {
74-
JsonNode jsonNode = objectMapper.readTree(json);
72+
JsonNode jsonNode = JsonUtils.MAPPER.readTree(json);
7573
if (jsonNode.isObject()) {
76-
return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
74+
return JsonUtils.MAPPER.readValue(json, new TypeReference<Map<String, Object>>() {});
7775
} else {
7876
// 非对象类型也返回空的 Map
7977
return new HashMap<>();

0 commit comments

Comments
 (0)