• dubbo rest返回值异常Incompatible types: declared root type


     1 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
     2 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
     3     at org.codehaus.jackson.map.ser.StdSerializerProvider._reportIncompatibleRootType(StdSerializerProvider.java:687) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
     4     at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:647) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
     5     at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:271) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
     6     at org.codehaus.jackson.map.ObjectWriter.writeValue(ObjectWriter.java:325) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
     7     at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:557) ~[jackson-jaxrs-1.9.13.jar:1.9.13]
     8     at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:137) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
     9     at org.jboss.resteasy.core.interception.ServerWriterInterceptorContext.writeTo(ServerWriterInterceptorContext.java:61) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
    10     at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:124) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
    11     at com.wjs.member.plugin.intercepter.RestContextInteceptor.aroundWriteTo(RestContextInteceptor.java:77) ~[com.wjs.dubbo-demo.service-180830-SNAPSHOT.jar:na]
    12     at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:129) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
    13     at org.jboss.resteasy.core.ServerResponseWriter.lambda$writeNomapResponse$2(ServerResponseWriter.java:140) ~[resteasy-jaxrs-3.6.0.Final.jar:3.6.0.Final]
    14     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类型,如下代码红色部分。

     1 @Override
     2     public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
     3 
     4 //        System.err.println("进入结果处理——aroundWriteTo");
     5 //        针对需要封装的请求对结构进行封装处理。这里需要注意的是对返回类型已经是封装类(比如:异常处理器的响应可能已经是封装类型)时要忽略掉。
     6         Object originalObj = context.getEntity();
     7         String wrapTag = context.getProperty("Not-Wrap-Result") == null ? "" : context.getProperty("Not-Wrap-Result").toString(); // 客户端显示提醒不要对返回值进行封装
     8         Boolean wraped = originalObj instanceof JsonResult; // 已经被封装过了的,不用再次封装
     9         if (StringUtils.isBlank(wrapTag) && !wraped){
    10             JsonResult<Object> result = new JsonResult<>(true, "执行成功");
    11             result.setData(context.getEntity());
    12             context.setEntity(result);
    13 //            以下两处set避免出现Json序列化的时候,对象类型不符的错误
    14             context.setType(result.getClass());
    15             context.setGenericType(result.getClass().getGenericSuperclass());
    16         }
    17         context.proceed();
    18         
    19     }
  • 相关阅读:
    简单理解Socket
    进程间8种通信方式详解
    底部漂浮DIV
    Table样式
    QQ授权登录
    C#_批量插入数据到Sqlserver中的四种方式
    Asp.Net_单点登录
    html之meta详解
    程序员常用工具
    工厂模式理解
  • 原文地址:https://www.cnblogs.com/loveyou/p/9549773.html
Copyright © 2020-2023  润新知