gpt4 book ai didi

java - 用于 API 不同部分的多个 ObjectMapper

转载 作者:行者123 更新时间:2023-11-30 02:21:45 27 4
gpt4 key购买 nike

目前,我的 Spring Boot 应用程序中有一个 @Bean 配置,如下所示:

@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

return mapper;
}

是否可以为 API 的不同部分使用不同的 ObjectMappers?例如,我感兴趣的是针对 API 的不同部分采用不同的属性命名策略。我希望我的 API 的一部分(例如 /api/v1/snake/**)能够与 SNAKE_CASE 及其一部分(例如 /api/v1/camel/**) 与 LOWER_CAMEL_CASE 一起使用。

谢谢!

最佳答案

如果 API 的两个部分不处理相同的域对象,您可以通过提供自己的 MappingJackson2HttpMessageConverter 实现(覆盖canReadcanWrite 方法。这是一个使用必须序列化的对象的基础包的示例:

自定义转换器:

public class CustomConverter extends MappingJackson2HttpMessageConverter {

private String basePackage;

public CustomConverter(String basePackage) {
this.basePackage = basePackage;
}

@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
if (contextClass.getName().startsWith(basePackage)) {
return super.canRead(type, contextClass, mediaType);
}
return false;
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
if (clazz.getName().startsWith(basePackage)) {
return super.canWrite(clazz, mediaType);
}
return false;
}
}

配置

@Configuration
public class Configuration {

@Bean
public MappingJackson2HttpMessageConverter camelCaseConverter() {
MappingJackson2HttpMessageConverter converter = new CustomConverter("package.camel");
converter.setObjectMapper(camelCaseObjectMapper());
return converter;
}

@Bean
public MappingJackson2HttpMessageConverter snakeCaseConverter() {
MappingJackson2HttpMessageConverter converter = new CustomConverter("package.snake");
converter.setObjectMapper(snakeCaseObjectMapper());
return converter;
}

@Bean
public ObjectMapper camelCaseObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);

return mapper;
}

@Bean
public ObjectMapper snakeCaseObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

return mapper;
}
}

关于java - 用于 API 不同部分的多个 ObjectMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46637479/

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