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

This commit is contained in:
wangyu 2024-07-05 17:38:57 +08:00
parent f4c0a4e678
commit 210fd09600
3 changed files with 32 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package group.flyfish.rest.configuration;
import group.flyfish.rest.configuration.configure.RestObjectMapperProvider;
import group.flyfish.rest.core.factory.DefaultHttpClientProvider;
import group.flyfish.rest.core.factory.HttpClientFactoryBean;
import group.flyfish.rest.core.factory.HttpClientProvider;
@ -11,6 +12,7 @@ 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.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -98,7 +100,8 @@ public class RestClientConfiguration {
* @return 结果
*/
@Bean
public JacksonUtil restJacksonUtil() {
public JacksonUtil restJacksonUtil(ObjectProvider<RestObjectMapperProvider> provider) {
provider.ifAvailable(bean -> JacksonUtil.setObjectMapper(bean.provide()));
return new JacksonUtil();
}
}

View File

@ -0,0 +1,19 @@
package group.flyfish.rest.configuration.configure;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* object mapper配置器
*
* @author wangyu
*/
@FunctionalInterface
public interface RestObjectMapperProvider {
/**
* 提供项目级别的object mapper
*
* @return 结果
*/
ObjectMapper provide();
}

View File

@ -66,6 +66,15 @@ public final class JacksonUtil {
mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
}
/**
* 设置jackson
*
* @param objectMapper 映射器
*/
public static void setObjectMapper(ObjectMapper objectMapper) {
JacksonUtil.mapper = objectMapper;
}
/**
* 修复不同环境下统一序列化表现
*