实例演示:在Java中运用CollectionType类
最编程
2024-07-26 09:33:20
...
实例1: updateTenants
import com.fasterxml.jackson.databind.type.CollectionType; //导入依赖的package包/类
@SneakyThrows
private void updateTenants(String key, String config) {
log.info("Tenants list was updated");
if (!TENANTS_LIST_CONFIG_KEY.equals(key)) {
throw new IllegalArgumentException("Wrong config key to update " + key);
}
assertExistsTenantsListConfig(config);
CollectionType setType = defaultInstance().constructCollectionType(HashSet.class, TenantState.class);
MapType type = defaultInstance().constructMapType(HashMap.class, defaultInstance().constructType(String.class), setType);
Map<String, Set<TenantState>> tenantsByServiceMap = objectMapper.readValue(config, type);
Set<TenantState> tenantKeys = tenantsByServiceMap.get(applicationName);
assertExistTenants(tenantKeys);
this.tenants = tenantKeys;
this.suspendedTenants = tenantKeys.stream().filter(tenant -> SUSPENDED_STATE.equals(tenant.getState()))
.map(TenantState::getName).collect(Collectors.toSet());
}
实例2: updateTenants
import com.fasterxml.jackson.databind.type.CollectionType; //导入依赖的package包/类
@SneakyThrows
private void updateTenants(String config) {
log.info("Tenants list was updated");
CollectionType setType = defaultInstance().constructCollectionType(HashSet.class, TenantState.class);
MapType type = defaultInstance().constructMapType(HashMap.class, defaultInstance().constructType(String.class), setType);
Map<String, Set<TenantState>> tenantsByServiceMap = objectMapper.readValue(config, type);
final Map<String, String> tenants = new HashMap<>();
for (TenantState tenant: tenantsByServiceMap.getOrDefault(applicationName, emptySet())) {
for (String host : hosts) {
tenants.put(tenant.getName() + "." + host, tenant.getName().toUpperCase());
}
}
this.tenants = tenants;
}
实例3: deserializeWithRetry
import com.fasterxml.jackson.databind.type.CollectionType; //导入依赖的package包/类
private Collection<Object> deserializeWithRetry(JsonParser p, DeserializationContext ctxt, JavaType contentType) throws IOException {
final CollectionType collectionType = ctxt.getTypeFactory().constructCollectionType(Collection.class, contentType);
try {
return p.getCodec().readValue(p, collectionType);
} catch (JsonMappingException e) {
// attempt to read the value as string
final String escapedString = p.getValueAsString();
// stop here if value could not be read
if (isNull(escapedString)) {
throw ctxt.instantiationException(Collection.class, "Read null value when attempting to deserialize " + collectionType.toString());
}
// un-escape double quotes
String unescapedString = escapedString.replaceAll("\"", "\"");
// and attempt to parse again
return new ObjectMapper().readValue(unescapedString, collectionType);
}
}
实例4: fromJson
import com.fasterxml.jackson.databind.type.CollectionType; //导入依赖的package包/类
/**
* 反序列化复杂Collection如List<Bean>
*
* @param <L> the type parameter
* @param <E> the type parameter
* @param jsonString the json string
* @param collectionClass the collection class
* @param elementClass the element class
* @return 转换失败时返回 null
*/
public static <L extends Collection<E>, E> L fromJson(String jsonString,
Class<L> collectionClass,
Class<E> elementClass) {
if (!StringUtils.hasText(jsonString)) {
return null;
}
try {
CollectionType type = mapper.getTypeFactory().constructCollectionType(collectionClass,
elementClass);
return mapper.readValue(jsonString, type);
} catch (Exception e) {
(new JsonUtil()).logger.error(e.getMessage(), e);
return null;
}
}
推荐阅读
-
实战演示:如何在Java中运用JGraphXAdapter类
-
实战演示:如何在Java中运用Verify类
-
实操示例:在Java中运用ChannelMatcher类
-
实操示例:在Java中运用WebSocketClientProtocolHandler类
-
在Java中,尝试性获取ReentrantLock实例的tryLock()和tryLock()方法示例及运用场景
-
TypeScript实战笔记:详解类与泛型的运用实例,学习如何在TypeScript中玩转泛型,及其在提升代码灵活性与安全性的角色 - 青训营教程
-
实操演示:Java中Cosine相似度计算类的运用示例
-
【2022新手指南】Java编程进阶之路 - 六、技术架构篇 ### MySQL索引底层解析与优化实战 - 你会讲解MySQL索引的数据结构吗?性能调优技巧知多少? - Redis深度揭秘:你知道多少?从基础到哨兵、主从复制全梳理 - Redis持久化及哨兵模式详解,还有集群搭建和Leader选举黑箱打开 - Zookeeper是个啥?特性和应用场景大公开 - ZooKeeper集群搭建攻略及 Leader选举、读写一致性、共享锁实现细节 - 探究ZooKeeper中的Leader选举机制及其在分布式环境中的作用 - Zab协议深入剖析:原理、功能与在Zookeeper中的核心地位 - RabbitMQ全方位解读:工作模式、消费限流、可靠投递与配置策略 - 设计者视角:RabbitMQ过期时间、死信队列与延时队列实践指南 - RocketMQ特性和应用场景揭示:理解其精髓与差异化优势 - Kafka详细介绍:特性及广泛应用于实时数据处理的场景解析 - ElasticSearch实力揭秘:特性概述与作为搜索引擎的广泛应用 - MongoDB认知升级:非关系型数据库的优势阐述,安装与使用实战教学 - BIO/NIO/AIO网络模型对比:掌握它们的区别与在网络编程中的实际应用 - Netty带你飞:理解其超快速度背后的秘密,包括线程模型分析 - 网络通信黑科技:Netty编解码原理与常用编解码器的应用,Protostuff实战演示 - 解密Netty粘包与拆包现象,怎样有效应对这一常见问题 - 自定义Netty心跳检测机制,轻松调整检测间隔时间的艺术 - Dubbo轻骑兵介绍:核心特性概览,服务降级实战与其实现益处 - Dubbo三大神器解读:本地存根与本地伪装的实战运用与优势呈现 ----------------------- 七、结语与回顾
-
实例演示:在Java中运用CollectionType类
-
实操示例:在Java中运用MySQLTransactionRollbackException类