gpt4 book ai didi

java - com.fasterxml.jackson.databind.exc.InvalidDefinitionException : No serializer found

转载 作者:行者123 更新时间:2023-12-02 08:50:35 34 4
gpt4 key购买 nike

出现错误,

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 Controller 调用以下函数时,

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])

如何解决?

最佳答案

您需要添加 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
}

关于java - com.fasterxml.jackson.databind.exc.InvalidDefinitionException : No serializer found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801310/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com