2012-03-15 22 views
5

私は/ wのテーブルBの多く-多くの関係があるときに、JSONはLazyInitializationException春@ResponseBody JSON循環参照

を投げているJPA 2.0 ORMを使用しています、JSON/XML応答を生成するために、春3.xの @ResponseBodyを使用しようとしています

"eager fetch"を指定すると、循環参照になります。

+0

これはあなたが探している答えではないかもしれませんが、私もこの問題にぶつかりました。関係を扱うカスタムの 'ObjectMapper'と' JsonSerializer'を書くことで解決しなければなりませんでした。 – bvulaj

+0

誰も私の問題に解決策を与えることができます、これはまだ私の開発を保持しています –

+0

私は2つのソリューションが掲載されて参照してください。あなたも試しましたか? – bvulaj

答えて

3

私は最近、同様の問題が発生しました:Jackson - serialization of entities with birectional relationships (avoiding cycles)

だから、解決策は、ジャクソン2.0へのアップグレード、およびクラスに以下の注釈を追加することです:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, 
        property = "@id") 
public class SomeEntityClass ... 

を次に、問題はその春ですJackson 2.0では動作しません。これは、次の方法で解決されました:

http://www.jarvana.com/jarvana/view/org/springframework/spring-web/3.0.0.RELEASE/spring-web-3.0.0.RELEASE-sources.jar!/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java?format=ok

しかしジャクソン2.0の代わりに、ジャクソン1からObjectMapperおよびその他のジャクソンクラスを使用*

<bean id="jacksonMessageConverter" 
      class="own.implementation.of.MappingJacksonHttpMessageConverter"/> 

<bean class="org.springframework.web.servlet.mvc 
      .annotation.AnnotationMethodHandlerAdapter"> 
     <property name="messageConverters"> 
      <list> 
       <ref bean="jacksonMessageConverter"/> 
      </list> 
     </property> 
     <property name="requireSession" value="false"/> 
    </bean> 

そしてown.implementation.of.MappingJacksonHttpMessageConverterはこれに基づいています

1

ORM管理オブジェクトをJSONにシリアル化していて、コントローラーにDB接続のハンドルがないため、すべての子の関連付けを初期化せずにLazyInitializationExceptionになっているようです。 2つの選択肢:

  1. はTOにORM-管理対象オブジェクトに変換し、JSON
  2. 判断
+0

私のシナリオでは、ユーザとグループには多数のリレーションシップがある。私はそれがサイクルに入ることを初期化する場合、子オブジェクトとしてのユーザー –

3

への変換のためのコントローラにそれを渡すDAO層

  • にオブジェクトの子アソシエーションのすべてを初期化しますあなたのコメントで、カスタムSerializerを作成してください。

    お客様のJsonSerializer。あなたは、シリアル化しようとしている各オブジェクトタイプに対してこれらを持つことができます。

    public class MyObjectJsonSerializer extends JsonSerializer<MyObject> { 
    
    @Override 
    public Class<MyObject> handledType() { 
        return MyObject.class; 
    } 
    
    @Override 
    public void serialize(MyObject myObject, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { 
        jgen.writeStartObject(); 
        jgen.writeNumberField("id", myObject.getId()); 
        // whatever else you need 
        jgen.writeEndObject(); 
    } 
    

    }

    あなたObjectMapper

    そして、あなたの春-config.xmlの

    public class MyObjectMapper extends ObjectMapper { 
    
    public MyObjectMapper() { 
        SimpleModule module = new SimpleModule("My Module", new Version(1, 0, 0, "SNAPSHOT")); 
        module.addSerializer(new MyObjectJsonSerializer()); 
    
        this.registerModule(module); 
    } 
    

    }。

    <mvc:annotation-driven> 
        <mvc:message-converters> 
         <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
          <property name="objectMapper" ref="myObjectMapper" /> 
         </bean> 
        </mvc:message-converters> 
    </mvc:annotation-driven> 
    
    <bean id="myObjectMapper" class="com.manne.app.objectmapper.MyObjectMapper" />