2017-03-24 9 views
0

私のプロジェクトでspringframeworksを使用しています.JSONの場合はjacksonを使用しています。 1つのリストを特定の方法でシリアル化する必要があります。つまり、特定のJSON出力が必要です。私はこのthreadの答えに従ったが、これは失敗する。Springframeworkの個別シリアライザ

@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(); 
    } 
} 

残念ながら、このアプローチは、例外で失敗します

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: {} 

適切に私のプロジェクトでそれを設定するには、どのように任意のヒント?

public UserDataSerializer() { 
    this(null); 
} 

を追加し、この問題の主な問題は、私はデフォルトはnullコンストラクタが含まれていなかったということでした

+0

あなたのクラス 'UserDataSerializer'に '@ bean'アノテーションを付けました –

答えて

1

、つまりは、この問題を解決しました。

関連する問題