发送用户定义的类给出 InvalidDefinitionException: No serializer found for class... and no properties found

Posted

技术标签:

【中文标题】发送用户定义的类给出 InvalidDefinitionException: No serializer found for class... and no properties found【英文标题】:Sending user defined class gives InvalidDefinitionException: No serializer found for class... and no properties discovered 【发布时间】:2019-04-03 02:40:29 【问题描述】:

我有一张地图,Map map = new HashMap(); 我在这张地图中存储了多种类型的数据。 1.) 原始类型 2.) 用户定义类的对象作为此映射中的值。当我将任何原始类型值转换为 Object 并存储在 Map 中时,它可以正常工作,但在用户定义的情况下则不行。在此地图中存储用户定义类型数据时,出现以下错误:-

**ERROR [WebExceptionHandler] Error occurred while serving request. Response is as [Response(

description=Type definition error: [simple type, class com.yyz.util.Abc];
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
  No serializer found for class com.yyz.util.Abc 
  and no properties discovered to create BeanSerializer
  (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

 (through reference chain: com.xyz.api.web.model.BuResponse["rows"]->java.util.ArrayList[0]->com.xyz.api.web.model.BuRow["children"]->java.util.ArrayList[11]->com.com.xyz.api.web.model.BuRow["[anySetter]"]->java.util.HashMap["column6"]), executionTime=0, responseEntity=<204 No Content,>,** responseStatus=ResponseStatus(hasError=true, responseKey=process-exception))]
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.yyz.util.Abc]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.yyz.util.Abc and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.xyz.api.web.model.BuResponse["rows"]->java.util.ArrayList[0]->com.com.xyz.api.web.model.BuRow["children"]->java.util.ArrayList[11]->com.com.xyz.api.web.model.BuRow["[anySetter]"]->java.util.HashMap["column6"])
      at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293) ~[spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:107) ~[spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:265) ~[spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:182) ~[spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82) ~[spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119) ~[spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871) ~[spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777) ~[spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) [spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881) [spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar!/:1.0.0.Final]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855) [spring-webmvc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar!/:1.0.0.Final]
      at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at com.blackrock.myworld.common.security.xss.XssFilter.doFilter(XssFilter.java:83) [myworld-common-2.9-SNAPSHOT.jar:2.9-SNAPSHOT]
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at com.blackrock.myworld.common.security.SecurityFilter.doFilter(SecurityFilter.java:73) [myworld-common-2.9-SNAPSHOT.jar:2.9-SNAPSHOT]
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at com.blackrock.myworld.api.framework.filter.ImitateLoginInInternalFilter.doFilter(ImitateLoginInInternalFilter.java:66) [classes:?]
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at com.blackrock.myworld.api.framework.filter.SimpleUserContextFilter.doFilter(SimpleUserContextFilter.java:42) [classes:?]
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at com.blackrock.myworld.common.framework.filter.UserContextFilter.doFilter(UserContextFilter.java:74) [myworld-common-2.9-SNAPSHOT.jar:2.9-SNAPSHOT]
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) [spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) [wildfly-undertow-8.2.0.Final.jar!/:8.2.0.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) [wildfly-undertow-8.2.0.Final.jar!/:8.2.0.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_20]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_20]
      at java.lang.Thread.run(Thread.java:745) [?:1.8.0_20]
**Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
No serializer found for class com.yyz.util.Abc and no properties discovered 
to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
(through reference chain: com.xyz.api.web.model.BuResponse["rows"]->java.util.ArrayList[0]->com.com.xyz.api.web.model.BuRow["children"]->java.util.ArrayList[11]->com.com.xyz.api.web.model.BuRow["[anySetter]"]->java.util.HashMap["column6"])**
      at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:312) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:718) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.AnyGetterWriter.getAndSerialize(AnyGetterWriter.java:62) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:723) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1396) ~[jackson-databind-2.9.2.jar:2.9.2]
      at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:913) ~[jackson-databind-2.9.2.jar:2.9.2]
      at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:286) ~[spring-web-5.0.1.RELEASE.jar:5.0.1.RELEASE]
      ... 68 more

谁能解释我为什么会收到此错误以及解决方案是什么?

@Setter
@Getter
@ToString
@JsonPropertyOrder( "numRecordsFound", "rowViewCount", "buyingUnitRows" )
public class BuResponse extends Response implements Serializable 

    private static final long serialVersionUID = -5269171275831114276L;

    private final List<BuRow> buRows = new ArrayList<>();

    private long numRecordsFound;

    private long rowViewCount;


@Getter
@Setter
@ToString
@EqualsAndHashCode(callSuper = false)
@JsonPropertyOrder( "properties", "children", "positionData" )
public class BuRow extends TreeRow 

    private static final long serialVersionUID = -9087386484833636274L;

    private List<BuRow> children;

    private Map<String, Object> positionData = new HashMap<>();

    private Map<String, Object> properties;

    public Map<String, Object> getPositionData() 
        return positionData;
    

    @JsonAnyGetter
    public Map<String, Object> getProperties() 
        return properties;
    

提前致谢!

【问题讨论】:

请发布您的com.yyz.util.Abc课程 Serializing with Jackson (JSON) - getting "No serializer found"?的可能重复 【参考方案1】:

由于您没有附加 com.yyz.util.Abc 类,因此无法说出确切原因,但请确保所有 getter 和 setter 都存在。

更多信息请参考:

Serializing with Jackson (JSON) - getting "No serializer found"?

【讨论】:

【参考方案2】:

将您的字段/getter 设为公开

异常说您的Abc 类是一个空bean,并且SerializationFeature.FAIL_ON_EMPTY_BEANS 已启用(默认情况下)。例如,这个类对于 jackson 序列化器来说是一个空 bean:

    public class Abc 
      boolean boo;
    

如果您至少有一个 public 字段或公共 getter,则可以避免异常:

    public class Abc 
      public boolean boo;
    

【讨论】:

以上是关于发送用户定义的类给出 InvalidDefinitionException: No serializer found for class... and no properties found的主要内容,如果未能解决你的问题,请参考以下文章

PHPUNIT 给出 COLOR_AUTO 未定义的类常量

用于发送和接收用户定义对象的套接字程序不起作用

在内核中访问用户空间结构的成员会给出错误的值

如何通过套接字发送.proto(协议缓冲区)中定义的类

如何在nodejs中发送api数据例如作为描述框给出

python利用企业微信api来进行发送自定义报警的类实现