dubbo rest返回值异常Incompatible types: declared root type
2018-08-28 17:26:02,208 [http-bio-9090-exec-1][][][][][] ERROR com.wjs.member.plugin.intercepter.ServiceExecutionInterceptor 116 - Incompatible types: declared root type ([map type; class java.util.Map, [simple type, class java.lang.String] -> [collection type; class java.util.List, contains [simple type, class com.wjs.member.clientVo.deduct.DeductCurVo]]]) vs com.wjs.common.web.JsonResult
org.codehaus.jackson.map.JsonMappingException: Incompatible types: declared root type ([map type; class java.util.Map, [simple type, class java.lang.String] -> [collection type; class java.util.List, contains [simple type, class com.wjs.member.clientVo.deduct.DeductCurVo]]]) vs com.wjs.common.web.JsonResult
at org.codehaus.jackson.map.ser.StdSerializerProvider._reportIncompatibleRootType(StdSerializerProvider.java:687) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:647) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:271) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
at org.codehaus.jackson.map.ObjectWriter.writeValue(ObjectWriter.java:325) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:557) ~[jackson-jaxrs-1.9.13.jar:1.9.13]
at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:137) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
at org.jboss.resteasy.core.interception.ServerWriterInterceptorContext.writeTo(ServerWriterInterceptorContext.java:61) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:124) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
at com.wjs.member.plugin.intercepter.RestContextInteceptor.aroundWriteTo(RestContextInteceptor.java:77) ~[com.wjs.dubbo-demo.service-180830-SNAPSHOT.jar:na]
at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:129) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
at org.jboss.resteasy.core.ServerResponseWriter.lambda$writeNomapResponse$2(ServerResponseWriter.java:140) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
at org.jboss.resteasy.core.interception.ContainerResponseContextImpl.filter(ContainerResponseContextImpl.java:395) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
在封装dubbo的rest请求返回值的时候,对于List和Map返回值,会出现如上错误。其原因是resteasy的AbstractWriterInterceptorContext调用JSON序列化的时候,传入的entity和genericType不一致,

codehaus.jackson的StdSerializerProvider类对entity和genericType比对不一致,并报错。

解决办法,在封装返回值的时候,修改WriterInterceptorContext的GenericType类型,如下代码红色部分。
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { // System.err.println("进入结果处理——aroundWriteTo");
// 针对需要封装的请求对结构进行封装处理。这里需要注意的是对返回类型已经是封装类(比如:异常处理器的响应可能已经是封装类型)时要忽略掉。
Object originalObj = context.getEntity();
String wrapTag = context.getProperty("Not-Wrap-Result") == null ? "" : context.getProperty("Not-Wrap-Result").toString(); // 客户端显示提醒不要对返回值进行封装
Boolean wraped = originalObj instanceof JsonResult; // 已经被封装过了的,不用再次封装
if (StringUtils.isBlank(wrapTag) && !wraped){
JsonResult<Object> result = new JsonResult<>(true, "执行成功");
result.setData(context.getEntity());
context.setEntity(result);
// 以下两处set避免出现Json序列化的时候,对象类型不符的错误
14 context.setType(result.getClass());
15 context.setGenericType(result.getClass().getGenericSuperclass());
}
context.proceed(); }
dubbo rest返回值异常Incompatible types: declared root type的更多相关文章
- java 代码执行cmd 返回值异常 (关于JAVA Project.waitfor()返回值是1)
关于JAVA Project.waitfor()返回值是1 0条评论 Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project ...
- 记一次用Linux curl命令获取Django url返回值异常的问题
问题描述: curl 检测 URL 返回值以判断服务器是否正常 原命令:curl -I -m 10 -o /dev/null -s -w %{http_code} --insecure $url 问题 ...
- Django的models方法返回值异常,待解决
class BookInfo(models.Model): #创建书本信息类,继承models.Model booktitle=models.CharField(max_length=20) book ...
- read from /dev/urandom 返回值异常
#include<stdio.h> #include<iostream> #include <fcntl.h> #include <sys/mman.h> ...
- Asp.net Core 异常日志与API返回值处理
需求: 1.对异常进行捕获记录日志 并且修改返回值给前端 解释: ILogger4是自定义的一个日志,更改它就好 解决方案1: 使用中间件进行异常捕获并且修改其返回值 public class Err ...
- Spring Aop 修改目标方法参数和返回值
一.新建注解 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Document ...
- ResponseBodyAdvice如何处理返回值是字符串的问题
项目中使用ResponseBodyAdvice同一封装返回格式,对于一般的类型都没有问题,但是处理字符串时,遇到了类型转换的问题,debug一步一步跟踪,原来是对于字符串的ContentType是“t ...
- linux shell 函数返回值问题(超过255)
最近再写一个shell测试的时候出现问题,函数返回值异常 用shell计算斐波那契数列数列,写了一个shell函数,然后调用的,验证的时候我只随便计算了几个数(10以内),确认结果是正确的就提交了,后 ...
- atitit.架构设计---方法调用结果使用异常还是返回值
atitit.架构设计---方法调用结果使用异常还是返回值 1. 应该返回BOOL类型还是异常 1 2. 最终会有四种状况,抛出异常.返回特殊值.阻塞.超时 1 3. 异常的优缺点点 1 4. jav ...
随机推荐
- 京东SDK模板卡盘效果实现代码
最近在做京东模板,因为是最新平台,好多功能都需要摸索,俺技术一般,摸索出一个简易的卡盘功能 ——————使用的是分类推荐模块哦! 本着共享的精神,俺将代码放到这儿了,各人请自便.(代码还不够完善, ...
- BZOJ1500:[NOI2005]维修数列
浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...
- UVA 10559 Blocks——区间dp
题目:https://www.luogu.org/problemnew/show/UVA10559 应该想到区间dp.但怎么设计状态? 因为连续的东西有分值,所以应该记录一下连续的有多少个. 只要记录 ...
- [原创]如何解决IE10下CkEditor报 --- SCRIPT5007: 无法获取未定义或 null 引用的属性“toLowerCase”
如何解决IE10下CkEditor报 --- SCRIPT5007: 无法获取未定义或 null 引用的属性“toLowerCase” 错误 如果你的IE是IE10,且不是运行在IE的兼容模式你也许会 ...
- .NETFramework-Timers:Timer
ylbtech-.NETFramework-Timers:Timer 1.程序集 System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b7 ...
- tp框架下,数据库和编辑器都是utf-8, 输出中文却还是乱码
输出: array(2) { [0]=> array(4) { ["id"]=> string(1) "1" ["user"]= ...
- 【249】◀▶IEW-Unit14
Unit 14 Money and Finance 线图写作技巧 1.Model1对应图片分析 The graph contains information about the price in US ...
- React-Redux之API
connect([mapStateToProps], [mapDispatchToProps], [mergeProps],[options]) 连接 React 组件与 Redux store. 连 ...
- In-App Purchase Configuration Guide for iTunes Connect---(一)----Introduction
Introduction In-App Purchase is an Apple technology that allows your users to purchase content and s ...
- 《精通Spring4.X企业应用开发实战》读后感第一章
Rod Johnson在2002年,编写了interface21框架,spring就是基于此.Spring于2004年3月24日发布了1.0 Spring遵循的理念“”好的设计优于具体实现,代码应易于 ...