InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discove

本文主要是介绍InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discove,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用springcloudgateway 搭建项目后,写个test验证返回响应,响应结果报如下错误:
 

{"timestamp": "2022-04-08T14:21:45.160+0000","path": "/tech/health/check","status": 500,"error": "Internal Server Error","message": "Type definition error: [simple type, class java.lang.Object]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: cn.quantgroup.yxm.gateway.mo.Result[\"data\"])"
}

具体错误信息为:


2022-04-08 22:02:21.415 [TID: N/A] [reactor-http-nio-3] ERROR o.s.b.a.w.r.e.AbstractErrorWebExceptionHandler -[dc04546c] 500 Server Error for HTTP GET "/tech/health/check"
org.springframework.core.codec.CodecException: Type definition error: [simple type, class java.lang.Object]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: cn.quantgroup.yxm.gateway.mo.Result["data"])at org.springframework.http.codec.json.AbstractJackson2Encoder.encodeValue(AbstractJackson2Encoder.java:178)at org.springframework.http.codec.json.AbstractJackson2Encoder.lambda$encode$0(AbstractJackson2Encoder.java:122)at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107)at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2070)at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:162)at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.set(Operators.java:1878)at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.onSubscribe(Operators.java:1752)at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:54)at reactor.core.publisher.MonoMapFuseable.subscribe(MonoMapFuseable.java:59)at reactor.core.publisher.MonoSwitchIfEmpty.subscribe(MonoSwitchIfEmpty.java:44)at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:150)at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1505)at reactor.core.publisher.MonoFlatMap$FlatMapInner.onNext(MonoFlatMap.java:241)at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:73)at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:204)at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:204)at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1505)at reactor.core.publisher.MonoIgnoreThen$ThenAcceptInner.onNext(MonoIgnoreThen.java:296)at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2070)at reactor.core.publisher.MonoIgnoreThen$ThenAcceptInner.onSubscribe(MonoIgnoreThen.java:285)at reactor.core.publisher.FluxFlatMap.trySubscribeScalarMap(FluxFlatMap.java:161)at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:53)at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:153)at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)at reactor.core.publisher.MonoPeekFuseable.subscribe(MonoPeekFuseable.java:74)at reactor.core.publisher.MonoPeekFuseable.subscribe(MonoPeekFuseable.java:74)at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:150)at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:67)at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:76)at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.innerNext(FluxConcatMap.java:275)at reactor.core.publisher.FluxConcatMap$ConcatMapInner.onNext(FluxConcatMap.java:849)at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:121)at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2070)at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:162)at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.set(Operators.java:1878)at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.onSubscribe(Operators.java:1752)at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:54)at reactor.core.publisher.MonoMapFuseable.subscribe(MonoMapFuseable.java:59)at reactor.core.publisher.Mono.subscribe(Mono.java:3710)at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:442)at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:212)at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:139)at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:63)at reactor.core.publisher.FluxConcatMap.subscribe(FluxConcatMap.java:121)at reactor.core.publisher.MonoNext.subscribe(MonoNext.java:40)at reactor.core.publisher.MonoSwitchIfEmpty.subscribe(MonoSwitchIfEmpty.java:44)at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)at reactor.core.publisher.MonoPeekTerminal.subscribe(MonoPeekTerminal.java:61)at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)at reactor.core.publisher.Mono.subscribe(Mono.java:3710)at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:172)at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)at reactor.core.publisher.MonoPeekFuseable.subscribe(MonoPeekFuseable.java:70)at reactor.core.publisher.MonoPeekTerminal.subscribe(MonoPeekTerminal.java:61)at reactor.netty.http.server.HttpServerHandle.onStateChange(HttpServerHandle.java:64)at reactor.netty.tcp.TcpServerBind$ChildObserver.onStateChange(TcpServerBind.java:234)at reactor.netty.http.server.HttpServerOperations.onInboundNext(HttpServerOperations.java:436)at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:141)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)at reactor.netty.http.server.HttpTrafficHandler.channelRead(HttpTrafficHandler.java:162)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:438)at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:323)at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:297)at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1408)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:930)at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:677)at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:612)at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:529)at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:491)at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:905)at java.lang.Thread.run(Thread.java:748)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: cn.quantgroup.yxm.gateway.mo.Result["data"])at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:313)at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1396)at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:913)at org.springframework.http.codec.json.AbstractJackson2Encoder.encodeValue(AbstractJackson2Encoder.java:174)... 95 common frames omitted

解决方案为:

方案1、设置ObjectMapper 为disable 为SerializationFeature.FAIL_ON_EMPTY_BEANS,并注入bean

@Configuration
public class BeanConfig {@Beanpublic ObjectMapper objectMapper(){return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);}
}或者@Configuration
public class BeanConfig {@Beanpublic Jackson2ObjectMapperBuilder objectMapper(){Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();builder.featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS);return builder;}
}

结果符合预期,验证输出结果为:

{"code": 200,"message": "","data": {}}

方案2:响应结果对象允许为空的字段对象上,添加JsonIgnore注解

import com.fasterxml.jackson.annotation.JsonIgnore;
@Data
public class Result<T> implements java.io.Serializable {/*** 0成功,永远默认成功*/private String code = "0000";/*** 业务错误码*/private String businessCode = ResultCode.SUCCESS.getCode();/*** 消息**/private String msg;/*** 详细消息**/private String detail;/*** 数据**/@JsonIgnoreprivate T data;private Boolean success;private String traceId;
}

验证输出结果为:

   {"code": 200,"message": ""
}

这篇关于InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discove的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/805375

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis