2012-03-01 12 views
2

Spring MVCとそのjsonサポートに1つ問題があります。私はいくつかのデータを取得するために1つのajax呼び出しを行い、私はその値をjson形式で取得する必要があります。また、REST APIのために使用されているので、エンティティでJABX注釈も使用しています。Spring MVCとJacksonのマッピングがjsonのルート要素を返さない

public class JaxbJacksonObjectMapper extends ObjectMapper { 

    public JaxbJacksonObjectMapper() { 
     final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); 

     this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true); 
     super.getDeserializationConfig().withAnnotationIntrospector(introspector); 

     this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true); 
     super.getSerializationConfig().withAnnotationIntrospector(introspector); 
    } 
} 
this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true); 

は、だから私はCodehausの1を拡張し、このようになります1 objectMapperを作成:

私はJacksonに含まれているルート値を取得することを読んだことが、私はこの方法を使用する必要があります

このマッパーを使用するには、私は次の行を設定しました:

<beans:bean id="customObjectMapper" class="com.test.package.config.JaxbJacksonObjectMapper" /> 

<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
    <beans:property name="objectMapper" ref="customObjectMapper" /> 
</beans:bean> 

<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
    <beans:property name="messageConverters"> 
     <beans:list> 
      <beans:ref bean="jsonMessageConverter"/> 
     </beans:list> 
    </beans:property> 
</beans:bean> 

そして、私のエンティティは次のようになります。

@XmlRootElement(name = "collection") 
public class Issuers { 
    private List<Issuer> issuers; 
} 

問題はSpring 3.1がブラウザに発行者にJSONオブジェクトを返すとき、それはcollectionルート要素が含まれていないということです。

どのようにすればこの問題を解決できますか?

ありがとうございます!

+0

http://stackoverflow.com/q/2435527/923560は、同様の課題を議論 – Abdull

+0

この問題を解決できました。あなたがしたことをお知らせください。 – Marco

答えて

0

「発行者」を一般的な所有者オブジェクトで囲みます。それはジャクソンとスプリングと一緒に仕事をしているときのことです。

class JSONResponse { 

    private Object collection; 

    public JSONResponse(Object collection) { 
     this.collection = collection; 
    } 
    public Object getCollection() { 
     return collection; 
    } 
} 

...

@RequestMappin(...) 
@ResponseBody 
public Object someHandler(...) { 
    ... 
    return new JSONResponse(issuers); 
} 
+0

これは一つのオプション、ありがとうございます。しかし私はすべての単一の要求でそれを変更する必要がないように、よりクリーンなソリューションを得たいと思います。しかし最悪の場合... –

+0

春の設定が正しくないようです。それはメッセンジャー変換器をオーバーライドしなかったので、私が書いたマッパーを取っていませんでした。それらを上書きする 、右スプリングの設定は次のとおり '<アノテーションドリブン> <メッセージ・コンバータ> <豆:Beanクラス= "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <豆:

+0

ここでルート要素が取得されていますが、 jabxの注釈ですが、クラスの名前です。 この問題を解決するにはどうすればよいですか?それは私が作ったジャクソンマッパーの問題に関係しているに違いありません... –

0

春の構成は右ではなかったようです。それはメッセンジャー変換器をオーバーライドしなかったので、私が書いたマッパーを取っていませんでした。

がそれらを無効にするには、右のバネの設定は次のとおりです。

<annotation-driven> 
    <message-converters> 
     <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
      <beans:property name="objectMapper" ref="customObjectMapper" /> 
     </beans:bean> 
    </message-converters> 
</annotation-driven> 

は今、私はルート要素を取得していますが、それはjabx注釈に指定された名前が、クラスの名前を取っていません。

新しいメソッド(withAnnotiationIntrospector)が正常に動作しないか、何か不足しているようです。しかし、廃止予定のものを使用する場合は、JABXラベルに定義されている適切なルート名を取得します。

public JaxbJacksonObjectMapper() { 
    final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); 

    this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true); 
    this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true); 

    // When using the non deprecated method (withAnnotationIntrospector), 
    // there are problems with JAXB/JSON parser so don't use right now 

    super.getDeserializationConfig().setAnnotationIntrospector(introspector); 
    super.getSerializationConfig().setAnnotationIntrospector(introspector); 
} 
3

方法withAnnotiationIntrospectorAnnotiationIntrospectorを設定していないようです。代わりにnew DeserializationConfig/SerializationConfigオブジェクトを返します(正しいAnnotiationIntrospector)。

だから、JaxbJacksonObjectMapperの私のバージョン:

public class JaxbJacksonObjectMapper extends ObjectMapper { 

    public JaxbJacksonObjectMapper() { 
     super(); 

     final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); 

     this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true); 
     this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true); 

     this.setDeserializationConfig(this.getDeserializationConfig().withAnnotationIntrospector(introspector)); 
     this.setSerializationConfig(this.getSerializationConfig().withAnnotationIntrospector(introspector)); 

    } 
} 

今では@XmlRootElement@XmlTransientおよびその他をサポートしています。1文を以下の本使用に

0

オルタナティブ:

setAnnotationIntrospector(introspector); 

代わりに二つの文を、次のあなたの場合は、

super.getDeserializationConfig().setAnnotationIntrospector(introspector); 
super.getSerializationConfig().setAnnotationIntrospector(introspector); 
関連する問題