一、@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);
}