(七)实体json序列化中的JsonIgnore

在spring boot项目中已经包含有json序列化的框架,具体在包com.fasterxml.jackson.annotation中,建议看看详细源码。

但在项目应用上还是会有一些坑会出现的,举个例子:

在一个复杂的业务模型中包含有200个字段,在查询列表时只查询其中某20个字段,在查询详情中需要把所有字段都查询出来

一般情况下,如果是开始做一个新功能,那么我们的设计应该类似是这样的:

model

---- QueryModel ,包含20个字段,响应查询列表结果

---- DetailModel extend POJO , 包含所有字段,响应查询实体结果

entity

---- POJO,包含所有字段

但维护从来都是一件蛋疼的事情,200个字段是迭代出来的,他们的逻辑是这样的:

entity

---- POJO,包含所有字段,响应查询列表和查询实体结果

这时候会发现一切蛋疼的原因就是直接把pojo拿来当model用了导致所有参数和结果无法拓展。为什么会这么蛋疼呢?原因不重要,如何解决才重要。

方案1:代码解耦,改造成model和entity分离

此方案的好处是一劳永逸,后续的拓展也比较轻松,弊端也显而易见,会发费许多时间去理解业务重构业务,而且一个稳定的系统一般不会尝试大范围的改造,万一改后出现一堆bug呢?

方案2:把不输出到页面的字段忽略掉

这时候用到注解 @JsonIgnore,该注解既可以作用在字段上也可以作用在方法上(另外两种先不说),可以看看源码和注释:

package com.fasterxml.jackson.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Marker annotation that indicates that the logical property that
 * the accessor (field, getter/setter method or Creator parameter
 * [of {@link JsonCreator}-annotated constructor or factory method])
 * is to be ignored by introspection-based
 * serialization and deserialization functionality.
 *<p>
 * Annotation only needs to be added to one of the accessors (often
 * getter method, but may be setter, field or creator parameter),
 * if the complete removal of the property is desired.
 * However: if only particular accessor is to be ignored (for example,
 * when ignoring one of potentially conflicting setter methods),
 * this can be done by annotating other not-to-be-ignored accessors
 * with {@link JsonProperty} (or its equivalents). This is considered
 * so-called "split property" case and allows definitions of
 * "read-only" (read from input into POJO) and "write-only" (write
 * in output but ignore on output)
 *<br>
 * NOTE! As Jackson 2.6, there is a new and improved way to define
 * `read-only` and `write-only` properties, using
 * {@link JsonProperty#access()} annotation: this is recommended over
 * use of separate <code>JsonIgnore</code> and {@link JsonProperty}
 * annotations.
 *<p>
 * For example, a "getter" method that would otherwise denote
 * a property (like, say, "getValue" to suggest property "value")
 * to serialize, would be ignored and no such property would
 * be output unless another annotation defines alternative method to use.
 *<p>
 * When ignoring the whole property, the default behavior if encountering
 * such property in input is to ignore it without exception; but if there
 * is a {@link JsonAnySetter} it will be called instead. Either way,
 * no exception will be thrown.
 *<p>
 * Annotation is usually used just a like a marker annotation, that
 * is, without explicitly defining ‘value‘ argument (which defaults
 * to <code>true</code>): but argument can be explicitly defined.
 * This can be done to override an existing `JsonIgnore` by explicitly
 * defining one with ‘false‘ argument: either in a sub-class, or by
 * using "mix-in annotations".
 */
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonIgnore
{
    /**
     * Optional argument that defines whether this annotation is active
     * or not. The only use for value ‘false‘ if for overriding purposes
     * (which is not needed often); most likely it is needed for use
     * with "mix-in annotations" (aka "annotation overrides").
     * For most cases, however, default value of "true" is just fine
     * and should be omitted.
     */
    boolean value() default true;
}

因此在使用的时候需要注意,如果只需要在查询的时候忽略,在保存的时候不忽略,那么需要在getter方法上注解@JsonIgnore,在setter方法上注解 @JsonProperty,(这时候如果使用的lombok就很尴尬了),举个例子:

public class TestEntity{private String name;

    /**
     * getter
     */
    @JsonIgnore
    public String getName() {
        return name;
    }

    /**
     * setter
     */
    @JsonProperty
    public void setName(String name) {
        this.name= name;
    }
}

然后在sql上从select *改成select 指定字段,解决查询列表问题。

然而,在查询单个实体详情的时候这些被忽略的字段也无法传到前端了,这是一条乍一看很理想的死胡同。

但从sql的角度上可以看出select 指定字段的时候,在反序列化到pojo的时候其他字段是没有值的,那么可以把方案换一下,让有值得字段序列化出去,没有值的不序列化

方案3:让有值得字段序列化出去,没有值的不序列化

sql上从select *改成select 指定字段

在pojo里用上@JsonInclude(JsonInclude.Include.NON_NULL)

此时既满足查询列表,又满足查询详情,解决问题。

原文地址:https://www.cnblogs.com/zqyx/p/10221244.html

时间: 2024-08-02 12:22:21

(七)实体json序列化中的JsonIgnore的相关文章

简单实体Json序列化(输出JSON的属性可变)

一.先看效果 可以看出 , 我们在序列化一个对像时, 只给出了 我们想要 输出的两个字段名,  实际实体有5个属性, 经过可变属性序列化后的JSON 结果,也是我们想要的. 二.正式上程序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.We

JSON序列化那点事儿

JSON序列化那点事儿 序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer是微软官方提供的一种方法,所以如果你用的是asp.net mvc,在Action中如果你返回的语句写的是”return Json(xxx);“,其实你用的就是JavaScriptSerializer方式.现在更多的人选择的是Json.net,因为它为用户提供了更加清晰地使用体

.Net深入实战系列—JSON序列化那点事儿

序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer是微软官方提供的一种方法,所以如果你用的是asp.net mvc,在Action中如果你返回的语句写的是”return Json(xxx);“,其实你用的就是JavaScriptSerializer方式.现在更多的人选择的是Json.net,因为它为用户提供了更加清晰地使用体验,清晰在哪?本文主要就

Java下用Jackson进行JSON序列化和反序列化(转)

Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行JSON和Java对象转换,下面给出一些Jackson的JSON操作方法. 一.准备工作 首先去官网下载Jackson工具包,下载地址http://wiki.fasterxml.com/JacksonDownload.Jackson有1.x系列和2.x系列,截止目前2.x系列的最新版本是2.9.0,2.x系列有3个jar包需要下载: jackson-cor

json序列化NHibernate的实体

在使用nhibernate时,想将实体对象序列化成json字符串,然后打印在日志中. 序列化时会出现问题,应该是因为这个实体被hibernate管理的原因.具体原因没有分析. 解决方案:为实体创建一个dto.使用autoMapper将实体映射到dto中.然后再序列化dto. 这样需要为每一个实体对象都创建一个dto,应该还有别的解决方案. json序列化NHibernate的实体,布布扣,bubuko.com

C#中JSON序列化和反序列化

json序列化和反序列化帮助类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.IO; using System.Text.RegularExpressions; using System.We

超高性能的json序列化之MVC中使用Json.Net

先不废话,直接上代码 Asp.net MVC自带Json序列化 1 /// <summary> 2 /// 加载组件列表 3 /// </summary> 4 /// <param name="departmentId">作业部/厂</param> 5 /// <param name="unitId">组件Id</param> 6 /// <param name="tag&quo

EF中Json序列化对象时检测到循环引用的解决办法

第一种方法:使用Newtonsoft.Json中的方法注释,在Json序列化的时候忽略导航属性 例:using Newtonsoft.Json; public class Users { public int Id { get; set; } public string LoginId { get; set; } public string LoginPwd { get; set; } [JsonIgnore] public virtual ICollection Roles { get; se

EntityFramework中Json序列化的循环引用问题解决--Newtonsoft.Json

1.在使用EF时,由于数据库主外键关联,将对象进行Json序列化时会遇到循环引用的问题 //EF 中由于数据库主外键关联,对象的序列化经常出现循环引用问题 //使用.Net 自带的序列化工具,序列化出现循环引用问题 List<student> list = _Context.students.ToList(); JavaScriptSerializer ser = new JavaScriptSerializer(); string str = ser.Serialize(list); Con