ParameterizedType 使用方法
2024-09-05 09:44:51
ParameterizedType 它是jdk提供的参数化类型,包括了如下
请求参数,和响应参数都是 参数话类型。记住凡是含有<T>中的都是参数话类型。
public static <T,U> void applyMethod(Map.Entry<T,U> mapEntry){ }
ParameterizedType 里的三个方法解释
Type getOwnerType(); 如果这个类是某个类的内部类,则拥有者就为某个类 Map.Entry<T,U> mapEntry Map为 ownerType,Entry为 rawType, T,U 为实际类
Type getRawType(); 标识原始类,如Response<Person> list的实现类,Response.class即为原始类型,
Type[] getActualTypeArguments() 包装类里的泛型,如Response<Person> Person.class 一般类反省类。
一般 要自己写一个ParameterizedType 的实现类(如果有现成的则可以不用自己实现),然后给json 转为对象,来使用
下面实例中的红色部分,为spring web MappingJackson2HttpMessageConverter 中的实现。
Person person=new Person();
person.setName("zhou");
person.setAddress("zhou");
person.setSex(1);
List<Person> list= Lists.newArrayList(person,person);
ObjectMapper objectMapper=new ObjectMapper();
String listString=objectMapper.writeValueAsString(list);
System.out.println(listString);
//JavaType javaType=objectMapper.getTypeFactory().constructParametrizedType(ArrayList.class, ArrayList.class, Person.class);
ParameterizedTypeImpl parameterizedTypeImpl=new ParameterizedTypeImpl(null, ArrayList.class, Person.class);
JavaType javaType=objectMapper.getTypeFactory().constructType(parameterizedTypeImpl);
List<Person> list1=objectMapper.readValue(listString, javaType);
for (Person person2 : list1) {
System.out.println(person2);
}
最新文章
- IBatis和Hibernate区别
- LeetCode Hamming Distance
- junit单元测试(keeps the bar green to keeps the code clean)
- 2014-10-28——iframe多层嵌套时获取元素总结
- iOS CoreData 的级联删除等操作
- BZOJ 3289 Mato的文件管理(莫队+离散化求逆序数)
- 跟我学 NHibernate (三)
- 理解C# Lazy<;T>;
- 转:Redis作者谈Redis应用场景
- SpringMVC 返回字符串
- DevExpress.LookUpEdit控件实现自动搜索定位功能 兼使用方法(looUpEdit可编辑)
- POJ 2585 Window Pains 题解
- linux学习(七)环境变量、cp、mv、cat,less,more,head,tail
- Kendo UI ASP.Net MVC 实现多图片及时显示加上传(其中有借鉴别人的代码,自己又精简了一下,如有冒犯,请多原谅!)
- SQL练习题题目
- 设计一个BCD码计数器。
- oracle批量删除某用户下的表
- iOS-iOS9系统SEGV_ACCERR问题处理【v3.6.3的一些bug修复】
- Python中的列表解析和生成器表达式
- SQL SERVER数据库维护与重建索引
热门文章
- 调用 url_launcher 模块打开外部浏 览器 打开外部应用 拨打电话 发送短信
- 【PAT甲级】1113 Integer Set Partition (25分)
- java位移运算符|And&;,操作二进制
- Jarvis OJ - Baby&#39;s Crack - Writeup
- (转载)Docker的boot2docker.iso镜像使用
- Docker 基本命令和使用
- 【NS-3学习】ns3-模拟基础:关键概念,日志,命令行参数
- 对于一些stl自定义比较函数
- 3.CRUD(增删改查)
- 微服务介绍和springCloud组件