使用feign进行服务调用,被调用方是调用成功的,在返回调用方的过程出现了问题,抛出错误sun.reflect.annotation.TypeNotPresentExceptionProxy。被调用方响应对象序列化是没有问题的,反序列化也是没问题的,但就是在调用方反序列化出现了问题。
调用方
BaseResponseDto refundLogBaseResponseDto = orderService.saveAndFlushRefundLog(refundLog);
@PostMapping(value = "/payment/saveAndFlushRefundLog",consumes = "application/json")
BaseResponseDto<RefundLog> saveAndFlushRefundLog(@RequestBody RefundLog refundLog);
被调用方
@PostMapping(value = ""/payment/saveAndFlushRefundLog",consumes = "application/json")
public BaseResponseDto<RefundLog> saveAndFlushRefundLog(@RequestBody RefundLog refundLog){
BaseResponseDto<RefundLog> refundLogBaseResponseDto = null;
try {
refundLogBaseResponseDto = refundService.saveAndFlushRefundLog(refundLog);
} catch (Exception e) {
return new BaseResponseDto<>(StatusCode.FAILURE);
}
return refundLogBaseResponseDto;
}
响应类
@Getter
@Setter
@ToString
public class BaseResponseDto<T> implements Serializable {
public BaseResponseDto() {
this.timestamp = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
}
public BaseResponseDto(Integer resultCode, String resultMessage) {
this.timestamp = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
this.resultCode = resultCode;
this.resultMessage = resultMessage;
}
public BaseResponseDto(Integer resultCode, String resultMessage, T responseBody) {
this.timestamp = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
this.resultCode = resultCode;
this.resultMessage = resultMessage;
this.responseBody = responseBody;
}
public BaseResponseDto(StatusCode statusCode) {
this.timestamp = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
this.resultCode = statusCode.getStatusCode();
this.resultMessage = statusCode.getInfo();
}
public BaseResponseDto(StatusCode statusCode, T responseBody) {
this.timestamp = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
this.resultCode = statusCode.getStatusCode();
this.resultMessage = statusCode.getInfo();
this.responseBody = responseBody;
}
/**
* 响应码
*/
private Integer resultCode;
/**
* 错误信息
*/
private String resultMessage;
/**
* 具体响应信息
*/
private T responseBody;
/**
* 时间戳
*/
private String timestamp;
}
debug过程出现问题
feign.codec.EncodeException: sun.reflect.annotation.TypeNotPresentExceptionProxy
被调用方响应对象的序列化和反序列化
在被调用方打印返回的BaseResponseDto<RefundLog>
这个对象,打印成json看是什么样的内容,然后在调用方把这个json反序列化成BaseResponseDto<RefundLog>
,看看能否成功反序列化,这样可以定位问题的原因。
如有帮助,请采纳,十分感谢!
接口已经定义泛型了,为什么你调用的时候接受返回结果没有泛型?
我们一般都是自定义序列方式不用默认的,feign会默认使用mvc的序列化,贴一下我的,希望对你有帮助
/**
* 自定义mvc序列化规则,使用传说中的mapper
*/
@Configuration
public class WebMvcConfig {
@Bean
ObjectMapper jacksonObjectMapper(){
return JsonUtil.mapper;
}
}
JsonUtil类->https://blog.csdn.net/qq_34031691/article/details/121495278?spm=1001.2014.3001.5502
把如图的注解去掉试试
参数的类型对吗,参数都改成string试试
建议返回 JOSNObject 类型,然后debug看下返回内容