zoukankan      html  css  js  c++  java
  • ParameterizedType 使用方法

    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);
            }
  • 相关阅读:
    重载与复写的区别
    Linux常用的网络命令
    JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
    Java中重载与复写的区别、super与this的比较
    JAVA中extends 与implements区别
    Android控件系列之Button以及Android监听器
    Could not find SDK_Root\tools\adb.exe 的解决方法
    路由知识普及和经验分享
    abort函数
    细说Cookie
  • 原文地址:https://www.cnblogs.com/z-test/p/9350613.html
Copyright © 2011-2022 走看看