json其他

Jackson注解(官方文档翻译)

2018-07-07  本文已影响35人  taojian

目前国内使用Jersey框架的人非常少,对于其注解的分析也不多,本人尝试着把官方文档翻译一遍,把所有的注解的了解一遍,有时候一个注解能够free掉一大块的代码呢。

翻译之后,你会发现几个重要点,官方的注解会区分为属性可用的注解,实体类可用的注解或者两者都可用的注解。

中文译文

​ 本页面列出常规用途的Jacakson2.0的注解,并按照功能分类。所有的注解包含简要介绍,以后将附上详细介绍的链接地址。

属性的别名

场景1:(目前对象名为storeTypeId,所以序列化出来的参数名就是storeTypeId,这里可以指定序列化为StoreTypeId这个名字,以满足需求。)
@JsonProperty("StoreTypeID")//不走驼峰命名(因为表单所限制)
private String storeTypeId;
或者
@JsonProperty(value = "StoreTypeID")//不走驼峰命名(因为表单所限制)
private String storeTypeId;

属性/实体 能否被序列化/反序列化(属性包含)

场景1:(实体类中某些属性只在代码中有用,序列化不想带出来,浪费流量)
@JsonIgnoreProperties(value={"person"}) ,这里是不让person属性序列化。

场景2:(有些值为NULL不想传过去,往往还得在SQL或者代码做循环判断,损耗性能)
@JsonInclude(Include.NON_NULL),为null的字段将不显示。

属性文档/元数据

反序列化/序列化的过程中操作

反序列化的过程中操作

序列化的过程中操作

类型处理

对象引用及标识

元注释

英语原文

原文地址

This page lists all general-purpose Jackson 2.0 annotations, grouped by functionality.

For older (Jackson 1.x) annotations, refer to FasterXML Wiki.

All annotations include a brief explanation, and (in near future!) a link to full explanation with basic usage examples.

NOTE: Contributions welcome!!!

Property Naming

Property Inclusion

Property documentation, metadata

Deserialization and Serialization details

Deserialization details

Serialization details

Type handling

Object references, identity

Meta-annotations

This group includes annotations used on other annotations.

Use with JAX-RS (DropWizard, Jersey)

Although value annotations are usable anywhere Jackson itself is, without extra work, there are some additional things to consider when using Jackson on a JAX-RS container.

Such containers require use of Jackson JAX-RS provider (or equivalent implementation of bit of glue to register Jackson for converting content between external format like JSON, and POJOs).

One specific limitation is that although Jackson can introspect annotations from within values it is passed, it does not have direct access to annotations on Resource Methods. Provider is handed these definitions, however, and it can use some of the annotations.

For more information, check out JAX-RS provider wiki, but short story is that following annotations are supported to some degree:

also note that annotations are NOT shared (that is, deserializer is NOT passed method annotations; nor is serializer passed parameter annotations), so in some cases you may need to violate DRY principle and add duplicate annotations

Related

It is also possible to use JAXB annotations in addition to or instead of these core annotations.

参考连接

http://blog.csdn.net/mooner_guo/article/details/42079173

http://blog.csdn.net/sdyy321/article/details/40298081

https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

上一篇 下一篇

猜你喜欢

热点阅读