gpt4 book ai didi

java - 如何在 Spring Boot 项目中添加新的 ObjectMapper

转载 作者:行者123 更新时间:2023-12-03 07:59:30 24 4
gpt4 key购买 nike

在我的 Spring Boot 项目中,我使用默认的 Jackson ObjectMapper。我想将新的 ObjectMapper 添加到 Spring Context 并开始在新的地方使用它,但也保留默认的。添加新的 @Bean 定义将覆盖默认的 ObjectMapper。如何添加新的 ObjectMapper Bean 而不覆盖前一个?

最佳答案

是的,@ConditionalOnMissingBean 是[很难]被破解的。通过一个简单的技巧(亚洲哲学),我们可以绕过这个问题/让它不再有问题:

将您的(1+,自动配置,@ConditionalOnMissing...)bean 包装在其他/自定义/“包装器”中。 (代价是:引用1+/思考差异/更复杂)

提到MappingJackson2HttpMessageConverter ( auto-config here ) 具有这种(内置)功能(和目的),可以根据“http 转换”映射到多个对象映射器。

因此,使用(通用的,例如基于 java.util.Map 的)类似的东西:

class MyWrapper<K, V> {
final Map<K, V> map;
public MyWrapper(Map<K, V> map) {
this.map = map;
}
public Map<K, V> getMap() {
return map;
}
}

我们可以接线:

@Bean
MyWrapper<String, ObjectMapper> myStr2OMWrapper(/*ObjectMapper jacksonOm*/) {
return new MyWrapper(Map.of(
// DEFAULT, jacksonOm,
"foo", fooMapper(),
"bar", barMapper()
));
}

..其中 fooMapper()barMapper() 可以引用(静态/实例)no-bean 方法:

private static ObjectMapper fooMapper() {
return new ObjectMapper()
.configure(SerializationFeature.INDENT_OUTPUT, true) // just a demo...
.configure(SerializationFeature.WRAP_ROOT_VALUE, true); // configure/set as see fit...
}
private static ObjectMapper barMapper() {
return new ObjectMapper()
.configure(SerializationFeature.INDENT_OUTPUT, false) // just a demo...
.configure(SerializationFeature.WRAP_ROOT_VALUE, false); // configure/set more...
}

(已经)测试/使用时间:

package com.example.demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoAppTests {

@Autowired
MyWrapper<String, ObjectMapper> my;
@Autowired
ObjectMapper jacksonOM;

@Test
void contextLoads() {
System.err.println(jacksonOM);
Assertions.assertNotNull(jacksonOM);
my.getMap().entrySet().forEach(e -> {
System.err.println(e);
Assertions.assertNotNull(e.getValue());
});
}
}

打印(例如)

...
com.fasterxml.jackson.databind.ObjectMapper@481b2f10
bar=com.fasterxml.jackson.databind.ObjectMapper@577bf0aa
foo=com.fasterxml.jackson.databind.ObjectMapper@7455dacb
...
Results:

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
...

抱歉,此测试不验证(单独)配置,而只是:(视觉上不同)不是空对象映射器。


如何启用(多个!)my.custom.jackson.*自动配置,是一个更复杂的问题...(它不像 my.custom.datasource.* config ;(

与:

@Bean
@Primary // ! for auto config, we need one primary (whether it is "spring.jackson" ... adjust;)
@ConfigurationProperties("spring.jackson")
JacksonProperties springJacksonProps() {
return new JacksonProperties();
}

@Bean
@ConfigurationProperties("foo.jackson")
JacksonProperties fooProps() {
return new JacksonProperties();
}

@Bean
@ConfigurationProperties("bar.jackson")
JacksonProperties barProps() {
return new JacksonProperties();
}

我们已经可以加载和区分(完整的)配置,例如:

spring.jackson.locale=en_US
spring.jackson.time-zone=UTC
# ... all of spring.jackson @see org.springframework.boot.autoconfigure.jackson.JacksonProperties
foo.jackson.locale=en_US
foo.jackson.time-zone=PST
# ... just for demo purpose
bar.jackson.locale=de_DE
bar.jackson.time-zone=GMT+1

并且(没问题)将它们(props)传递给相应的(static [foo|bar]Mapper)方法......但是然后呢? (如果你擅长的话,你可以停止阅读这里!:)

不幸的是according ("state of art") code (将 JacksonProperties 与“om builder”连接)不是公开的(即不可扩展/可插入)。

相反,自动配置提供(如果没有定义/@ConditionalOnMissingBean):

  • 一个原型(prototype) Jackson2ObjectMapperBuilder bean,它(每次请求时):

所以最简单的方法似乎(最新)是:

  • 钢/采用the code (未-/实现Jackson2ObjectMapperBuilderCustomizer)
  • 根据构建器/映射器构建(从“被盗”+属性),如认为合适。

例如(在生产之前回顾+测试!)非接口(interface),返回一个Jackson2ObjectMapperBuilder,模仿自动配置,不应用(其他)定制器/-ation:

// ...
import com.fasterxml.jackson.databind.Module; // !! not java.lang.Module ;)
// ...
private static class MyStolenCustomizer {

private final JacksonProperties jacksonProperties;
private final Collection<Module> modules;
// additionally need/want this:
private final ApplicationContext applicationContext;
// copy/adopt from spring-boot:
private static final Map<?, Boolean> FEATURE_DEFAULTS = Map.of(
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false,
SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false
);

public MyStolenCustomizer(
ApplicationContext applicationContext,
JacksonProperties jacksonProperties,
Collection<Module> modules
) {
this.applicationContext = applicationContext;
this.jacksonProperties = jacksonProperties;
this.modules = modules;
}
// changed method signature!!
public Jackson2ObjectMapperBuilder buildCustom() {
// mimic original (spring-boot) bean:
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.applicationContext(applicationContext);
// without (additional!) customizers:
if (this.jacksonProperties.getDefaultPropertyInclusion() != null) {
builder.serializationInclusion(this.jacksonProperties.getDefaultPropertyInclusion());
}
if (this.jacksonProperties.getTimeZone() != null) {
builder.timeZone(this.jacksonProperties.getTimeZone());
}
configureFeatures(builder, FEATURE_DEFAULTS);
configureVisibility(builder, this.jacksonProperties.getVisibility());
configureFeatures(builder, this.jacksonProperties.getDeserialization());
configureFeatures(builder, this.jacksonProperties.getSerialization());
configureFeatures(builder, this.jacksonProperties.getMapper());
configureFeatures(builder, this.jacksonProperties.getParser());
configureFeatures(builder, this.jacksonProperties.getGenerator());
configureDateFormat(builder);
configurePropertyNamingStrategy(builder);
configureModules(builder);
configureLocale(builder);
configureDefaultLeniency(builder);
configureConstructorDetector(builder);
// custom api:
return builder; // ..alternatively: builder.build();
}
// ... rest as in https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java#L223-L341

要连接模块,我们可以(希望像最初一样)依赖:

@Autowired
ObjectProvider<com.fasterxml.jackson.databind.Module> modules

像这样初始化它们:

@Bean
MyStolenCustomizer fooCustomizer(ApplicationContext context, @Qualifier("fooProps") JacksonProperties fooProperties, ObjectProvider<Module> modules) {
return new MyStolenCustomizer(context, fooProperties, modules.stream().toList());
}

@Bean
MyStolenCustomizer barCustomizer(ApplicationContext context, @Qualifier("barProps") JacksonProperties barProperties, ObjectProvider<Module> modules) {
return new MyStolenCustomizer(context, barProperties, modules.stream().toList());
}

..并像这样使用它们:

@Bean
MyWrapper<String, Jackson2ObjectMapperBuilder> myStr2OMBuilderWrapper(
@Qualifier("fooCustomizer") MyStolenCustomizer fooCustomizer,
@Qualifier("barCustomizer") MyStolenCustomizer barCustomizer) {
return new MyWrapper(
Map.of(
"foo", fooCustomizer.buildCustom(),
"bar", barCustomizer.buildCustom()
)
);
}

...避免“双重定制”/保持 JacksonAutoConfiguration 启用/完整/Activity 。

问题:时间/更新(/外部代码)!

关于java - 如何在 Spring Boot 项目中添加新的 ObjectMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74874123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com