fix: 优化object mapper项目配置注入

This commit is contained in:
wangyu 2024-05-27 17:45:28 +08:00
parent f19c8b550c
commit f4c0a4e678
5 changed files with 28 additions and 5 deletions

View File

@ -13,7 +13,7 @@
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.8</version>
<version>1.1.9</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
@ -22,7 +22,7 @@
<java.version>1.8</java.version>
<commons-collection.version>4.4</commons-collection.version>
<commons.lang.version>2.6</commons.lang.version>
<sdk.version>1.1.8</sdk.version>
<sdk.version>1.1.9</sdk.version>
</properties>
<packaging>pom</packaging>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.8</version>
<version>1.1.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.8</version>
<version>1.1.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -9,6 +9,7 @@ import group.flyfish.rest.registry.proxy.support.RestArgumentResolver;
import group.flyfish.rest.registry.proxy.support.RestArgumentResolverComposite;
import group.flyfish.rest.registry.proxy.support.resolvers.*;
import group.flyfish.rest.utils.DataUtils;
import group.flyfish.rest.utils.JacksonUtil;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -90,4 +91,14 @@ public class RestClientConfiguration {
);
return new RestArgumentResolverComposite(resolvers);
}
/**
* 更新原生的jackson object mapper
*
* @return 结果
*/
@Bean
public JacksonUtil restJacksonUtil() {
return new JacksonUtil();
}
}

View File

@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.text.SimpleDateFormat;
@ -24,7 +26,7 @@ import java.util.Optional;
*/
public final class JacksonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
private static ObjectMapper mapper = new ObjectMapper();
static {
// =========================================================================
@ -64,6 +66,16 @@ public final class JacksonUtil {
mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
}
/**
* 修复不同环境下统一序列化表现
*
* @param objectMappers jackson om
*/
@Autowired
public void setObjectMapper(ObjectProvider<ObjectMapper> objectMappers) {
objectMappers.ifAvailable(om -> mapper = om);
}
public static <T> T fromJson(final String json) {
return readValue(json, new TypeReference<T>() {
});