Uso springframeworks en mi proyecto y para JSON usamos jackson . Una lista debe serializarse de una manera específica, es decir, debe tener una salida JSON específica. Seguí las respuestas de este hilo pero esto falla.
@JsonSerialize(using = UserDataSerializer.class) List<TestObj> lstTest;y
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(); } }pero desafortunadamente este enfoque falla con una excepción:
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: {}¿Alguna pista sobre cómo configurarlo correctamente en mi proyecto?
El principal problema con este problema fue que no incluí un constructor nulo predeterminado, es decir, agregué
public UserDataSerializer() { this(null); }resolvió este problema.