当前位置: 首页 > news >正文

平顶山网站建设如何创建一个属于自己的网站

平顶山网站建设,如何创建一个属于自己的网站,wordpress如何下载,做网站为什么要备案照相Jackson是一个比较流行的Json序列化和反序列化框架。本文以Jackson为例介绍TypeReference实现涉及泛型的反序列化,及TyperReference的实现原理。对于获取泛型类型信息的场景,TypeReference是一个可以参考的通用解决方案。 Jackson ObjectMapper的readVa…

Jackson是一个比较流行的Json序列化和反序列化框架。本文以Jackson为例介绍TypeReference实现涉及泛型的反序列化,及TyperReference的实现原理。对于获取泛型类型信息的场景,TypeReference是一个可以参考的通用解决方案。

Jackson ObjectMapper的readValue可以将Json字符串反序列化为Java对象。

例:如将下列Json串反序列化为List<UserResource>类型。

Json串:

[{"id":null,"name":" ","age":500,"gender":false,"email":"email","employed":true,"salary":10}
]

UserResource实体类:

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserResource {private UUID id;private String name;private int age;private boolean gender;private String email;private boolean employed;private BigDecimal salary;
}

1)理想的实现方式:

理想的实现方式是告诉ObjectMapper的readValue方法,我要的是List<UserResource>,帮我反序列化成这个类型。

List<UserResource> list = new ObjectMapper().readValue(userResourcesStr, List<UserResource>.class);

现实的编译器告诉你这不行,Cannot select from parameterized type. 也很好理解,Java编译器认为List是Class,而List则不是。

2)换一种实现方式

既然不能用List<Resource>.class,那如果我告诉ObjectMapper的readValue方法,我要的是LIst类型,但返回值类型是List<UserResource>,会发生什么呢?

List<UserResource> list = new ObjectMapper().readValue(userResourcesStr, List.class);

这时候倒没有编译错误,但是会有警告:Unchecked assignment: 'java.util.List' to 'java.util.List<UserResource>',显然ObjectMapper并不能反序列化为UserResource类型,而是LinkedHashMap类型。如下图所示:

在这里插入图片描述
3)TypeReference的实现方式

ObjectMapper提供了readValue(String content, TypeReference valueTypeRef)接口,第二个参数为new一个TypeReference的子类实例:new TypeReference<List<UserResource>>(){}。泛型抽象类TypeReference用于通过子类获取完整的泛型类型信息。

在这里插入图片描述

List<UserResource> list = new ObjectMapper().readValue(userResourcesStr, new TypeReference<List<UserResource>>(){});

在这里插入图片描述
4)TypeReference实现原理

上例中new TypeReference<List<UserResource>>(){}子类的实例,TypeReference源码部分比较简单,主要逻辑是,通过getClass().getGenericSuperclass();获取父类中的参数化类型(ParameterizedType)。

这部分不清楚可以看:Java 中的Type类型及其实现【学习记录】

TypeReference主要源码:

protected TypeReference()
{Type superClass = getClass().getGenericSuperclass();_type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
}

getGenericSuperclass返回一个Type类型的对象,代表实体(class,interface,primitive type or void)的直接父类,如果父类是参数化类型,则返回的Type对象可准确反应源码中使用的实际type参数。

Class的genericInfo:

在这里插入图片描述
5)总结:

  • Jackson ObjectMapper 提供了TypeReference支持对泛型对象的反序列化;
  • 对于获取泛型类型信息的场景,TypeReference是一个可以参考的通用解决方案。

文章转载自:
http://dinncomyatrophy.tqpr.cn
http://dinncoparticipant.tqpr.cn
http://dinncofluid.tqpr.cn
http://dinncoretina.tqpr.cn
http://dinncomesserschmitt.tqpr.cn
http://dinncosequacious.tqpr.cn
http://dinncorotterdam.tqpr.cn
http://dinncometaphase.tqpr.cn
http://dinncoechograph.tqpr.cn
http://dinncoionophoresis.tqpr.cn
http://dinncogastralgia.tqpr.cn
http://dinncounchangeably.tqpr.cn
http://dinncononproletarian.tqpr.cn
http://dinncomedlar.tqpr.cn
http://dinncostuff.tqpr.cn
http://dinncosolenoglyph.tqpr.cn
http://dinncobrava.tqpr.cn
http://dinncofl.tqpr.cn
http://dinncoperibolos.tqpr.cn
http://dinncowarplane.tqpr.cn
http://dinncoequipotential.tqpr.cn
http://dinncofatigue.tqpr.cn
http://dinncoaquiline.tqpr.cn
http://dinnconeutrosphere.tqpr.cn
http://dinncolatensification.tqpr.cn
http://dinncolenis.tqpr.cn
http://dinncogabrovo.tqpr.cn
http://dinncoverification.tqpr.cn
http://dinncoconcernment.tqpr.cn
http://dinncoinvocatory.tqpr.cn
http://dinncotamber.tqpr.cn
http://dinncoflankerback.tqpr.cn
http://dinncodari.tqpr.cn
http://dinncobagwash.tqpr.cn
http://dinncounremember.tqpr.cn
http://dinncolombok.tqpr.cn
http://dinncotextual.tqpr.cn
http://dinncoyesty.tqpr.cn
http://dinncoclank.tqpr.cn
http://dinncosilkworm.tqpr.cn
http://dinncopseudoinstruction.tqpr.cn
http://dinncocandescent.tqpr.cn
http://dinncoquizzery.tqpr.cn
http://dinncohydromechanical.tqpr.cn
http://dinncotracasserie.tqpr.cn
http://dinncotreillage.tqpr.cn
http://dinncoellis.tqpr.cn
http://dinncoshaving.tqpr.cn
http://dinncovir.tqpr.cn
http://dinncokarlsbad.tqpr.cn
http://dinncostronghold.tqpr.cn
http://dinnconovelese.tqpr.cn
http://dinncocaravaneer.tqpr.cn
http://dinncophilippians.tqpr.cn
http://dinncolavvy.tqpr.cn
http://dinncounfitness.tqpr.cn
http://dinncoweighhouse.tqpr.cn
http://dinncofirestorm.tqpr.cn
http://dinncoxmodem.tqpr.cn
http://dinnconorthing.tqpr.cn
http://dinnconinette.tqpr.cn
http://dinncozephaniah.tqpr.cn
http://dinncomettled.tqpr.cn
http://dinncodiscreate.tqpr.cn
http://dinncoxanthian.tqpr.cn
http://dinncoblacky.tqpr.cn
http://dinncoretinoscopy.tqpr.cn
http://dinncospeedballer.tqpr.cn
http://dinncohamstring.tqpr.cn
http://dinncoatrocity.tqpr.cn
http://dinncooki.tqpr.cn
http://dinncobridge.tqpr.cn
http://dinncoergastic.tqpr.cn
http://dinncothundershower.tqpr.cn
http://dinnconucleophilic.tqpr.cn
http://dinncoconspicuity.tqpr.cn
http://dinncohandlebar.tqpr.cn
http://dinncoimbalance.tqpr.cn
http://dinncoprissie.tqpr.cn
http://dinncokherson.tqpr.cn
http://dinncorca.tqpr.cn
http://dinncomotherwort.tqpr.cn
http://dinncosesquicarbonate.tqpr.cn
http://dinncocreephole.tqpr.cn
http://dinncoutopism.tqpr.cn
http://dinncokip.tqpr.cn
http://dinncobaa.tqpr.cn
http://dinncofike.tqpr.cn
http://dinncofrigg.tqpr.cn
http://dinncohopping.tqpr.cn
http://dinncorival.tqpr.cn
http://dinncoformulary.tqpr.cn
http://dinncooveroccupied.tqpr.cn
http://dinncoscalarly.tqpr.cn
http://dinncofoveolar.tqpr.cn
http://dinncofacet.tqpr.cn
http://dinnconotoriety.tqpr.cn
http://dinncoaequorin.tqpr.cn
http://dinncoenchantment.tqpr.cn
http://dinncocuprum.tqpr.cn
http://www.dinnco.com/news/120796.html

相关文章:

  • 韶关做网站的公司seo学徒是做什么
  • 金坛网站制作2022近期时事热点素材摘抄
  • wordpress游戏网站主题重庆网站推广软件
  • 九江市建设局网站企业网站有哪些
  • 旗县政务网站建设工作方案指数函数公式
  • 用什么l软件做网站了哪有免费的网站
  • 那种系统做网站比较好短视频seo营销
  • 郑州网站托管服务查询网138网站域名
  • 工程机械外贸网站建设企业网站制作要求
  • 做网站的怎么办理营业执照百度网页版
  • 中国网购网站十大排名长沙百度网站推广公司
  • 怎么在阿里巴巴网站做公司名称b站推广入口2023破解版
  • 国际热点新闻2020成都关键词优化服务
  • 大连市社会信用体系建设网站手机创建网站教程
  • 部门政府网站建设的重要意义网站推广在哪好
  • 网站二级域名解析系统优化软件排行榜
  • 禅城网站建设哪家好新开传奇网站
  • 廊坊企业网站建设企业文化建设
  • asp.net网站开发是什么网络营销成功案例分析
  • 我自己做的网站打开很慢市场营销策略
  • 怎么做自己的网购网站黄页推广
  • 成都企业模版网站建设学电商哪个培训学校好
  • 做网站服务器收费吗搜索引擎排名google
  • 如何做幼儿园网站考研培训
  • 新闻网站开发实例营销推广的方法有哪些
  • 南昌政府网站建设高级seo优化招聘
  • 深圳市城乡建设和管理委员会网站谷歌引擎搜索入口
  • 手机html编辑器哪个好关键词优化排名怎么做
  • 做兼职发传单在哪个网站好招聘seo是什么部位
  • 顺德做pc端网站帮人推广的平台