【问题标题】:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer foundcom.fasterxml.jackson.databind.exc.InvalidDefinitionException:未找到序列化程序
【发布时间】:2020-07-03 04:38:09
【问题描述】:

出现错误,

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.app.server.data.MyUser and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:404) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1429) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:921) ~[jackson-databind-2.10.2.jar:2.10.2]

当从 Spring Boot 控制器调用以下函数时,

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class JdbcUserRepository implements UserRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<MyUser> findAll() {

        return jdbcTemplate.query( "select * from users", (rs, rowNum) ->
            new MyUser(
                    rs.getLong("user_id"),
                    rs.getString("user_nid"),
                    rs.getString("user_name"),
                    rs.getString("user_duid")
            )
        );
    }
}

型号:

public class MyUser {

    Long userId;
    String userNid;
    String userName;
    String userDuid;

    public MyUser(Long userId, String userNid, String userName, String userDuid) {
        super();
        this.userId = userId;
        this.userNid = userNid;
        this.userName = userName;
        this.userDuid = userDuid;
    }
}

浏览器出现错误,

There was an unexpected error (type=Internal Server Error, status=500).
Type definition error: [simple type, class com.app.server.data.MyUser]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.app.server.data.MyUser and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])

我该如何解决?

【问题讨论】:

  • MyUser 有 getter 和 setter 吗?
  • 不,我用 MyUser 类更新了帖子
  • 所以和 getter 序列化可以访问数据
  • 如果您还想拥有来自 JSON 的对象,请同时添加 getter 和 setter
  • 谢谢西蒙,它解决了这个问题。您可以将其作为答案以供进一步参考。

标签: java spring-boot controller jackson


【解决方案1】:

您需要添加让 Jackson 可以访问对象状态的 getter 和 setter。

public class MyUser {

    Long userId;
    String userNid;
    String userName;
    String userDuid;

    public MyUser(Long userId, String userNid, String userName, String userDuid) {
        super();
        this.userId = userId;
        this.userNid = userNid;
        this.userName = userName;
        this.userDuid = userDuid;
    }

    // public getters and setters
}

【讨论】:

    【解决方案2】:

    使用(安装)lombok 为您创建 getter 和 setter。将 @Data 注释放在您的班级上方。 @Data class MyUser { int id; }

    【讨论】:

      猜你喜欢
      • 2022-09-24
      • 2019-12-14
      • 2015-09-18
      • 2021-07-30
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      相关资源
      最近更新 更多