springmvc+hibernate+restful 使用多对一many-to-one 出现getOutputStream() has already been called for this

2 篇文章 0 订阅
1 篇文章 0 订阅

@[TOC](springmvc+hibernate+restful 使用多对一many-to-one 出现getOutputStream() has already been called for this response)

**查了很多百度,都没有介绍到点上。特别标识出来供大家参考

因为restful接口返回的都是json,所以springmvc 需要调用fastjson进行序列化

重点强调 @JsonIgnoreProperties(value={“hibernateLazyInitializer”,“handler”,“fieldHandler”}) 告诉json序列化时,那个属性不需要进行序列化。

在此标记不生成json对象的属性

因为jsonplugin用的是java的内审机制.hibernate会给被管理的pojo加入一个hibernateLazyInitializer属性,
jsonplugin会把hibernateLazyInitializer也拿出来操作,
并读取里面一个不能被反射操作的属性就产生了这个异常.  
不过我用的是jackson来转json,
所以想到了用annotation来排除hibernateLazyInitializer 这个属性

在你的pojo类声明加上:
@JsonIgnoreProperties(value={"hibernateLazyInitializer"})  
@JsonIgnoreProperties就是标注 哪个属性 不用转化为json的

model StudyProgress :

    public class StudyProgress  {
    private Integer id;
    private Integer dataId; 
    @JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
    private HWData hwData; 
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getDataId() {
        return dataId;
    }

    public void setDataId(Integer dataId) {
        this.dataId = dataId;
    }

    

    public HWData getHwData() {
        return hwData;
    }

    public void setHwData(HWData hwData) {
        this.hwData = hwData;
    }

文件映射

 <hibernate-mapping> 
   <class name="com.studyonline.mgr.model.StudyProgress" table="studyProgress">
        <id name="id" type="java.lang.Integer">
            <column name="id"/>
            <generator class="identity"/>
        </id>
        <property name="dataId">
            <column name="dataId"/>
        </property>
      

        <many-to-one name="hwClass" class="com.studyonline.mgr.model.HWClass" column="classId"
                     insert="false" update="false" cascade="none" lazy="no-proxy"  fetch="select"></many-to-one>

    </class>
</hibernate-mapping>

spring 配置:使用interceptor+OpenSessionInViewInterceptor hibernate延长session有效期

    <!-- 建立视图内拦截器来解决JPA中访问延迟加载属性时产生的无会话异常 -->
    <!-- LazyInitializationException: could not initialize proxy no session -->
    <!-- 此拦截器会注入到servlet配置中的DefaultAnnotationHandlerMapping中 -->
    <bean name="openSessionInterceptor"
          class="org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/front**"/>
            <ref bean="openSessionInterceptor"/>
        </mvc:interceptor>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43598053

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值