Guli Mall public return R plus generic can not get data, data is empty after deserialization

According to the implementation, I found that a null pointer error was reported. After positioning, I found that: after
insert image description here
the remote call, after adding generics to the unified return object R, the data value cannot be obtained. Here, the data is empty. When debugging
insert image description here
finds out whether there is stock, This vos has a value. Here, there is a problem when setting vos into data:
insert image description here
you can see that after setData is passed, the return value of R only contains msg and code, and there is no data.

Here you need to modify the data method of R in the public module, comment out the original data construction parameters, and add the following code
insert image description here

public R setData(Object data) {
    
    
		put("data",data);
		return this;
	}

	//利用fastjson进行反序列化
	public <T> T getData(TypeReference<T> typeReference) {
    
    
		Object data = get("data");	//默认是map
		String jsonString = JSON.toJSONString(data);
		T t = JSON.parseObject(jsonString, typeReference);
		return t;
	}

	//利用fastjson进行反序列化
	public <T> T getData(String key,TypeReference<T> typeReference) {
    
    
		Object data = get(key);	//默认是map
		String jsonString = JSON.toJSONString(data);
		T t = JSON.parseObject(jsonString, typeReference);
		return t;
	}

After transforming the R object in this way, you can use the setData method to set the parameter data in the R object normally:
insert image description here

When taking out the value, you can get the set data data in the following ways:
insert image description here

 R skusHasStock = wareFeignService.getSkusHasStock(skuIdList);
      
 TypeReference<List<SkuHasStockVo>> typeReference = new TypeReference<List<SkuHasStockVo>>() {
    
    };
   List<SkuHasStockVo> data = skusHasStock.getData(typeReference);

Guess you like

Origin blog.csdn.net/weixin_42260782/article/details/128483301