一.问题
Spring Boot + Spring Cloud + FastJSON
A服务中如下代码:
import com.alibaba.fastjson.JSON;
import com.dafycredit.common.model.Result;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
@Service
public class CreditCertificateServiceImpl implements CreditCertificateService {
private static final Logger LOGGER = LoggerFactory.getLogger(BankCardServiceImpl.class);
@Autowired
WechatExecuteManage wechatExecuteManage;
@Override
public Result<List<Map<String, Object>>> listCertificates(@RequestBody Map<String, Object> paramMap) {
Map<String, Object> wxParamMap = new HashedMap<>();
wxParamMap.put("personId",paramMap.get("idPerson"));
String result = wechatExecuteManage.request("http://localhost:8188/wechat-web/credits/getClosingProof", wxParamMap, Boolean.TRUE);
return (Result<List<Map<String, Object>>>) convertWxApiResult(result);
}
}
package com.dafycredit.common.model;
import java.io.Serializable;
/**
* 接口返回通用类
* @param <T>
*/
public class Result<T> implements Serializable {
private static final long serialVersionUID = -2804195259517755050L;
public static final int SUCCESS_CODE = 0;
public static final String ERROR_MSG = "fail";
private int code; // 状态码
private String message; // 说明信息
private String result; // result结果,快速判断
private T data; // 结果数据
public Result() {
this(SUCCESS_CODE, "成功","success",null);
}
public Result(int code, String msg) {
this(code, msg, ERROR_MSG, null);
}
public Result(T data) {
this(SUCCESS_CODE, "成功", "success", data);
}
public Result(int code, String message, String result, T data) {
this.code = code;
this.message = message;
this.result = result;
this.data=data;
}
get/set()...
@Override
public String toString() {
return "Result [code=" + code + ", message=" + message + ", result=" + result + ", data=" + data + "]";
}
}
返回结果示例:
{
"code": "0",
"message": "获取结清证明合同成功",
"result": "fail",
"data": {}
}
B服务中调用A服务:
@RestController
@PreAuthorize("hasAuthority('USER')")
@RequestMapping(V1_PERSON_CENTER_BASE + "/credit")
public class CreditCertificateController {
public static final Logger LOGGER = LoggerFactory.getLogger(CreditCertificateController.class);
@Autowired
private CreditCertificateService creditCertificateService;
@LogAnnotation(desc = "web-获取结清证明列表")
@ValidateGroup(fileds = {
@ValidateFiled(index = 0, filedName = FnValidateConstants.ID_PERSON, notNull = true, msg = "IdPerson不能为空")})
@RequestMapping(value = "/listCertificates", method = RequestMethod.POST)
public Result<List<Map<String, Object>>> listCertificates(@RequestBody Map<String, Object> paramMap) {
return creditCertificateService.listCertificates(paramMap);
}
}
结果报错:
{
"code": "-1",
"message": "Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@1af3ed51; line: 1, column: 79] (through reference chain: com.dafycredit.common.model.Result[\"data\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@1af3ed51; line: 1, column: 79] (through reference chain: com.dafycredit.common.model.Result[\"data\"])",
"result": "fail",
"data": null
}
反编译
泛型使用注意:
使用泛型之后,不要再使用强制转换类型的手段处理数据,尽量使用明细的数据对象,子泛型类就用 <? super T>。