gpt4 book ai didi

java - Springframework 中的单独序列化器

转载 作者:行者123 更新时间:2023-12-02 13:36:04 26 4
gpt4 key购买 nike

我在项目中使用 springframeworks,对于 JSON,我们使用 jackson。一个列表应以特定方式序列化,即应具有特定的 JSON 输出。我遵循了这个thread的答案但这失败了。

@JsonSerialize(using = UserDataSerializer.class)
List<TestObj> lstTest;

public class UserDataSerializer extends StdSerializer<TestObj> {

protected UserDataSerializer(Class<TestObj> t) {
super(t);
}

@Override
public void serialize(TestObj value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeObjectField(value.getName(), value.getValue());
gen.writeEndObject();
}
}

但不幸的是,这种方法失败了,但有一个异常(exception):

Servlet.service() for servlet [test3backend] in context with path [/test] threw exception [Request processing failed; nested exception is org.springframework.beans.f
actory.UnsatisfiedDependencyException: Error creating bean with name 'me.test.entities.UserDataSerializer': Unsatisfied dependency expressed through constructor paramete
r 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.Class<me.test.entities.TestObj>' av
ailable: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}] with root cause
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.Class<at.me.test.entities.TestObj>' available: expected at leas
t 1 bean which qualifies as autowire candidate. Dependency annotations: {}

有任何关于如何在我的项目中正确配置它的提示吗?

最佳答案

这个问题的主要问题是我没有包含默认的 null 构造函数,即添加

public UserDataSerializer() {
this(null);
}

解决了这个问题。

关于java - Springframework 中的单独序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42996334/

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