SpringMVC+Hibernate控制器返回json时的坑

坑1:
Spring3.x和Spring4.xSpring5.x使用的不是同样的jar包
正确的如下:

spring-mvc.xml
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean
            class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json; charset=UTF-8</value>
                    <value>application/x-www-form-urlencoded; charset=UTF-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
pom.xml
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
    </dependency>

坑二,如果返回的数据是直接从Hibernate获取到的,那么需要在类上加一个注解:@JsonIgnoreProperties({“hibernateLazyInitializer”, “handler”})
因为Hibernate会自动插入一个属性
坑三:死循环要去掉一个字段,本来以为这样写@JsonIgnoreProperties({“hibernateLazyInitializer”, “handler”,”一个Set属性”})
结果不起作用,查了半天,特么的,这个用法只支持1.x的jackson版本!虽然2.x也有这个类,但是没有了这个功能!
换成1.x版本后,发现jackson的命名有问题,
一个好好的字段,给我反射成这样,依然报错…
这里写图片描述
这里写图片描述

我决定还是放弃1.x版本吧….

解决办法:最好使用VO返回吧,坑太多了,而且,myeclipse逆向工程生成的实体,首字母是T的话,有坑,有的字段不遵循驼峰命名!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

不甘心:终于找到了终极解决:
使用的最新版的jackson
没有用1.x
用的上面的
类上加:@JsonIgnoreProperties(value={“hibernateLazyInitializer”,”handler”})
一对多关系中get方法上加:@JsonIgnore

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值