@JsonIgnore和@GeneratedValue

本文深入探讨了@JsonIgnore注解在JSON序列化过程中的应用,如何通过该注解选择性地忽略对象的某些属性,以及@GeneratedValue注解在主键生成策略中的作用,包括SEQUENCE和TABLE策略的具体使用场景。
摘要由CSDN通过智能技术生成

@JsonIgnore注解

1、返回 json 字符串时候,忽略该字段,举栗子

我封装一个user对象,里面有name,age这2属性,在age上面加了@JsonIgnore注解,把数据返回前端,json格式的user对象中并不存在age属性;

@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;
}

注释说,一般用于marker annotation人名的注释

**
 * 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".

2、@GeneratedValue 用于主键生成

@Target({METHOD, FIELD})
@Retention(RUNTIME)

public @interface GeneratedValue {

    /**
     * (Optional) The primary key generation strategy
     * that the persistence provider must use to
     * generate the annotated entity primary key.
     */
    GenerationType strategy() default AUTO;

    /**
     * (Optional) The name of the primary key generator
     * to use as specified in the {@link SequenceGenerator} 
     * or {@link TableGenerator} annotation.
     * <p> Defaults to the id generator supplied by persistence provider.
     */
    String generator() default "";
}

源码注释如下

* Provides for the specification of generation strategies for 
 * the values of primary keys. The <code>GeneratedValue</code> 
 * annotation may be applied to a primary key property or
 * field of an entity or mapped superclass in conjunction with 
 * the {@link Id} annotation.
 *
 * <pre>
 *
 * Example 1:
 *
 *     &#064;Id
 *     &#064;GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
 *     &#064;Column(name="CUST_ID")
 *     public Long getId() { return id; }
 *
 *     Example 2:
 *
 *     &#064;Id
 *     &#064;GeneratedValue(strategy=TABLE, generator="CUST_GEN")
 *     &#064;Column(name="CUST_ID")
 *     Long id;
 * </pre>
 *
 * @since Java Persistence 1.0
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值