第一步:
打開https://github.com/FasterXML/jackson-datatype-hibernate,下載對應hibernate版本的jackson-datatype-hibernate的jar包。
第二部:
創建一個類繼承ObjectMapper(com.fasterxml.jackson.databind包下的);
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
public class HibernateAwareObjectMapper extends ObjectMapper {
public HibernateAwareObjectMapper() {
registerModule(new Hibernate4Module());
}
}
第三步:
在springmvc的配置文件中修改節點。
<mvc:annotation-driven>
<mvc:message-converters>
<!-- Use the HibernateAware mapper instead of the default -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.boleprint.util.HibernateAwareObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
至此完成所有配置完成。直接運行就能完美運行。