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);
        }
    }
    
  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));
        }
    }
    
  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>>(){});
        }
    }
    
  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);
    	}
    	
    	@Test
    	public void exchangeMap(){
    		String url = "http://localhost:8080/user-service/hello/index" ;
    		ParameterizedTypeReference<Map<String,Object>> typeReference =
    				new ParameterizedTypeReference<>() {} ;
    		ResponseEntity<Map<String,Object>> exchange = 
    			 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null), typeReference);
    		Map<String,Object> retValue = exchange.getBody();
    		log.info("ret value : {}", retValue);
    	}
    
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值