• TypeReference使用笔记


    1. JSON字符串反序列化对象(单个对象)

      @Slf4j
      public class TypeReferenceTest{
          @Test
          public void parseObject(){
              String jsonContent = "{\"code\":\"200\",\"message\":\"error message\",\"data\":\"hello world\"}" ;
              TypeReference<RestResponse<String>> typeReference = new TypeReference<String>(){} ;
              RestResponse<String> response = JSON.parseObject(jsonContent, typeReference);
              log.info("====> response : {}", response);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    2. ObjectMapper字符串反序列化(集合)

      @SpringBootTest(classes = DynamicNacosApplication.class)
      public class ObjectMapperTest {
          @Autowired
          private ObjectMapper objectMapper ;
      
          @Test
          public void collectionType() throws JsonProcessingException {
              String content = "...." ;
              CollectionType collectionType = objectMapper.getTypeFactory()
                      .constructCollectionType(ArrayList.class, RouteDefinition.class);
              List<RouteDefinition> routeDefinitions = objectMapper.readValue(content, collectionType);
              routeDefinitions.forEach(item -> log.info("item : {}", item));
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    3. ObjectMapper字符串反序列化(集合)

      @SpringBootTest(classes = DynamicNacosApplication.class)
      public class ObjectMapperTest {
          @Autowired
          private ObjectMapper objectMapper ;
      
          @Test
          public void collectionType() throws JsonProcessingException {
              String content = "...." ;
       	   // other method
              List<RouteDefinition> infoCmds = 
       			objectMapper.readValue(jsonData, new TypeReference<List<RouteDefinition>>(){});
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    4. RestTemplate返回值泛型转换

      @Slf4j
      public class ParameterizedTypeReferenceTest {
          private RestTemplate restTemplate = new RestTemplate();
      
          @Test
          public void exchange(){
              String url = "http://localhost:8080/user-service/hello/index" ;
              ParameterizedTypeReference<RestResponse<String>> typeReference =
                      new ParameterizedTypeReference<>() {} ;
              ResponseEntity<RestResponse<String>> exchange = 
                   restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null), typeReference);
              RestResponse<String> retValue = exchange.getBody();
              log.info("ret value : {}", retValue);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
  • 相关阅读:
    PAT数字&字符串处理
    CMAKE使用记录
    高手必备 | Revit插件到底哪个好?区别是什么?
    最新WooCommerce教程指南-如何搭建B2C外贸独立站
    【React】第五部分 高阶函数和函数的柯里化
    WPF图表库LiveCharts的使用
    scrapy的selenium跑不起来
    看谷歌浏览器源码,为什么p标签和div标签为块元素
    python基础-面向对象
    [第一章]1.3 等可能概型
  • 原文地址:https://blog.csdn.net/yichengjie_c/article/details/132942315