由于org.codehaus.jackson.map.JsonMappingException转换java.util.UnmodifiableRandomAccessList时出现HttpMessageNotWritableException

时间:2014-08-30 13:10:56

标签: java json spring cassandra jackson

我从Cassandra

获得了以下List<ColumnFamilyDefinition>数据
ThriftCfDef[
            keyspace=sample_data,
            name=application_data,
            columnType=STANDARD,
            comparatorType=me.prettyprint.hector.api.ddl.ComparatorType@757e1d2f,
            subComparatorType=<null>,
            comparatorTypeAlias=,
            subComparatorTypeAlias=,
            comment=,
            rowCacheSize=0.0,
            rowCacheSavePeriodInSeconds=0,
            keyCacheSize=0.0,
            readRepairChance=0.1,
            columnMetadata=[
                ThriftColumnDef[
                    name=java.nio.HeapByteBuffer[
                        pos=3208lim=3216cap=14616
                    ],
                    validationClass=org.apache.cassandra.db.marshal.UTF8Type,
                    indexType=<null>,
                    indexName=<null>
                ],
                ThriftColumnDef[
                    name=java.nio.HeapByteBuffer[
                        pos=3271lim=3280cap=14616
                    ],
                    validationClass=org.apache.cassandra.db.marshal.UTF8Type,
                    indexType=<null>,
                    indexName=<null>
                ]
            ],
            gcGraceSeconds=864000,
            keyValidationClass=org.apache.cassandra.db.marshal.UTF8Type,
            keyValidationAlias=,
            defaultValidationClass=org.apache.cassandra.db.marshal.BytesType,
            id=3656,
            maxCompactionThreshold=32,
            minCompactionThreshold=4,
            memtableOperationsInMillions=0.0,
            memtableThroughputInMb=0,
            memtableFlushAfterMins=0,
            keyCacheSavePeriodInSeconds=0,
            replicateOnWrite=true,
            compactionStrategy=org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy,
            compactionStrategyOptions={

            },
            compressionOptions={
                sstable_compression=org.apache.cassandra.io.compress.SnappyCompressor
            },
            mergeShardsChance=0.0,
            rowCacheProvider=<null>,
            keyAlias=<null>,
            rowCacheKeysToSave=0
        ]

由于ThriftColumnDef

,我收到以下错误
HttpMessageNotWritableException : Could not write JSON: (was java.nio.BufferUnderflowException) (through reference chain: java.util.HashMap["sample_data"]->java.util.UnmodifiableRandomAccessList[2]->me.prettyprint.cassandra.service.ThriftCfDef["columnMetadata"]->java.util.ArrayList[0]->me.prettyprint.cassandra.service.ThriftColumnDef["name"]->java.nio.HeapByteBuffer["long"]); nested exception is org.codehaus.jackson.map.JsonMappingException: (was java.nio.BufferUnderflowException) (through reference chain: java.util.HashMap["sample_data"]->java.util.UnmodifiableRandomAccessList[2]->me.prettyprint.cassandra.service.ThriftCfDef["columnMetadata"]->java.util.ArrayList[0]->me.prettyprint.cassandra.service.ThriftColumnDef["name"]->java.nio.HeapByteBuffer["long"])

以下是完整的堆栈跟踪

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.nio.BufferUnderflowException) (through reference chain: java.util.HashMap["sample_data"]->java.util.UnmodifiableRandomAccessList[2]->me.prettyprint.cassandra.service.ThriftCfDef["columnMetadata"]->java.util.ArrayList[0]->me.prettyprint.cassandra.service.ThriftColumnDef["name"]->java.nio.HeapByteBuffer["long"]); nested exception is org.codehaus.jackson.map.JsonMappingException: (was java.nio.BufferUnderflowException) (through reference chain: java.util.HashMap["sample_data"]->java.util.UnmodifiableRandomAccessList[2]->me.prettyprint.cassandra.service.ThriftCfDef["columnMetadata"]->java.util.ArrayList[0]->me.prettyprint.cassandra.service.ThriftColumnDef["name"]->java.nio.HeapByteBuffer["long"])
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:143)
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.writeWithMessageConverters(AnnotationMethodHandlerAdapter.java:1031)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.handleResponseBody(AnnotationMethodHandlerAdapter.java:989)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:938)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:438)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.jackson.map.JsonMappingException: (was java.nio.BufferUnderflowException) (through reference chain: java.util.HashMap["sample_data"]->java.util.UnmodifiableRandomAccessList[2]->me.prettyprint.cassandra.service.ThriftCfDef["columnMetadata"]->java.util.ArrayList[0]->me.prettyprint.cassandra.service.ThriftColumnDef["name"]->java.nio.HeapByteBuffer["long"])
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
    at org.codehaus.jackson.map.ser.SerializerBase.wrapAndThrow(SerializerBase.java:131)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:183)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:430)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
    at org.codehaus.jackson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:304)
    at org.codehaus.jackson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:254)
    at org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:142)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:430)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
    at org.codehaus.jackson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:304)
    at org.codehaus.jackson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:254)
    at org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:142)
    at org.codehaus.jackson.map.ser.MapSerializer.serializeFields(MapSerializer.java:287)
    at org.codehaus.jackson.map.ser.MapSerializer.serialize(MapSerializer.java:212)
    at org.codehaus.jackson.map.ser.MapSerializer.serialize(MapSerializer.java:23)
    at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:600)
    at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:280)
    at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1352)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:140)
    ... 24 more
Caused by: java.nio.BufferUnderflowException
    at java.nio.Buffer.nextGetIndex(Buffer.java:478)
    at java.nio.HeapByteBuffer.getLong(HeapByteBuffer.java:387)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:467)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:402)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
    ... 44 more

如何将此thrift数据转换为有效JSON作为对最终用户的回复?

1 个答案:

答案 0 :(得分:0)

问题在于下面的字节数据需要转换为String。

name=java.nio.HeapByteBuffer[
  pos=3208lim=3216cap=14616
]

下面的方法可用于将字节数据转换为String。

Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
String data = "";
int old_position = byteBuffer.position();
try {
    data = decoder.decode(java.nio.HeapByteBuffer[pos=3208lim=3216cap=14616]).toString();
    byteBuffer.position(old_position);
    System.out.println(data);
} catch (CharacterCodingException e) {
    e.printStackTrace();
}

希望这有助于解决其他问题。