@JsonIgnore、@JSONField(serialize = false)不起作用

一、@JsonIgnore不起作用原因

参考:实体类中@JsonIgnore注解不起作用 - 百度文库

1.@JsonIgnore作⽤:在json序列化时将pojo中的⼀些属性忽略掉,标记在属性或者⽅法上,返回的json数据即不包含该属性。
2.@JsonIgnore不起作⽤的原因(可对照参考):
①注意导包:com.fasterxml.jackson.annotation.JsonIgnore
②注意属性的命名规范,需要使⽤驼峰规则。

本人遇到的失效错误代码:

@JsonIgnore
@TableField(exist = false)
private Integer vInput;

修改后生效(需要正确使用驼峰规则):

@JsonIgnore
@TableField(exist = false)
private Integer volInput;

 3. 也包括:

@JsonIgnoreProperties({ "volInput", "volOutput"})

二、@JSONField(serialize = false)不起作用原因

参考:Springboot项目中使用FastJson注解@JSONField(serialize = false)无效 - 代码先锋网

分析原因
Sprintboot默认是通过jackson来转换JSON的,@JSONField(serialize = false)是FastJson的注解,Springboot使用jackson进行转JSON的时候并不会扫描这个注解。

解决方法
在Springboot启动类中添加如下配置(没试过)

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值