Error:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: xx.xx.xx.db.xx.xx.xx.xx, could not initialize proxy - no Session at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:588) [hibernate-core-5.3.3.Final.jar:5.3.3.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:207) [hibernate-core-5.3.3.Final.jar:5.3.3.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:151) [hibernate-core-5.3.3.Final.jar:5.3.3.Final] at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261) [hibernate-core-5.3.3.Final.jar:5.3.3.Final] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:97) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3893) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3207) [jackson-databind-2.9.5.jar:2.9.5]
Config to clear error:
Reference URL:
https://coderleaf.wordpress.com/2016/11/07/jackson-serialization-and-hibernate-lazy-loading/
@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport{ @Override protected void extendMessageConverters( List<HttpMessageConverter<?>> converters ) { for ( HttpMessageConverter<?> converter : converters ) { if ( converter instanceof MappingJackson2HttpMessageConverter ) { MappingJackson2HttpMessageConverter jacksonConverter = (MappingJackson2HttpMessageConverter) converter; ObjectMapper objectMapper = jacksonConverter.getObjectMapper(); //--- register hibernateModule in MappingJackson2HttpMessageConverter.objectMapper objectMapper.registerModule(new Hibernate4Module()); //--- other configurations jacksonConverter.setPrettyPrint( true ); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); } } super.configureMessageConverters(converters); } }
xml config to clear error
Reference URL:
https://stackoverflow.com/questions/43946323/spring-and-mappingjackson2httpmessageconverter-and-registermodule
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="featuresToDisable">
<array>
<util:constant
static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />
</array>
</property>
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
</bean>
</property>
<property name="modulesToInstall"
value="
com.fasterxml.jackson.datatype.jdk8.Jdk8Module,
com.fasterxml.jackson.datatype.jsr310.JavaTimeModule,
com.fasterxml.jackson.module.paramnames.ParameterNamesModule" />
</bean>
No comments:
Post a Comment