Jackson在spring MVC中的应用 --- 20161023

一、jackson介绍

Overview of The Jackson API

Jackson Api contains a lot of functionalities to read and build json using java. It has very powerful data binding capabilities and provides a framework to serialize custom java objects to json string and deserialize json string back to java objects. Json written with jackson can contain embedded class information that helps in creating the complete object tree during deserialization.

jackson的git主页: https://github.com/FasterXML/jackson

jackson的文档主页:https://github.com/FasterXML/jackson-docs

 

好了,看到这就结束了,其他的可以去官网看文档了。

 

二、jackson实际应用

1. 首先你可以轻易的使用jackson来将java对象转换为json字符串,并且可以设置它的格式。例如下面。可以定制要转化的时间的格式。或者将json字符串转化java对象。

 

{"title":"Kind Of Blue","links":["link1","link2"],
"songs":["So What","Flamenco Sketches","Freddie Freeloader"],
"artist":{"name":"Miles Davis","birthDate":-1376027600000}}

 

 

 

 

{
  "title" : "Kind Of Blue",
  "links" : [ "link1" , "link2" ],
  "songs" : [ "So What", "Flamenco Sketches", "Freddie Freeloader" ],
  "artist" : {
    "name" : "Miles Davis",
    "birthDate" : -1376027600000
  }
}

 

 

2. 在spring MVC中,我们经常需要@ResponseBody编写接口来返回一些json格式的数据,这时我看到过两种情况:

(1)有人用google的Gson将结果转换为json再返回字符串。显然他不知道spring会自动用jackson将你返回的对象转换为json。

(2)有人用jackson但是,例如日期,空字符串的处理,不需要的字符串等。则进行手动的干预,已达到想要的效果。其实,jackson的注解功能可以轻松的完成这些功能。

例如:

 @JsonIgnoreProperties({"prop1", "prop2"}) 加在POJO上,当你返回该对象时,spring自动转化为json时

就会忽略这里列出来的属性。

 

@JsonFormat @JsonFormat(pattern="yyyy-MM-dd"), 不需要任何多余配置,直接转化为想要的格式,不过存

在时区问题,默认是按照标准格林威治时间。

 

 

解决办法:
@JsonFormat(pattern="yyyy-MM-dd")
 public Date getRegistDate() {
  return this.registDate;
 }
改成
@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
 public Date getRegistDate() {
  return this.registDate;
 }

 

 

下面列出了几个有用的注解,当然jackson的注解远不止这些,还有去空,设置为null时返回“”而不是null等,都在上面的文档中等着你去探索。

 

Jackson相关:

1、@JsonProperty 

2、@JsonIgnoreProperties

此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。

 

3、@JsonIgnore

此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。

 

4、@JsonFormat

此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")

 

5、@JsonSerialize

此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。

 参考:http://blog.csdn.net/xcy13638760/article/details/8962085

6、@JsonDeserialize

此注解用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码,类似于上面的

 

@JsonSerialize

Property Naming

  • @JsonProperty (also indicates that property is to be included) is used to indicate external property name, name used in data format (JSON or one of other supported data formats)
    • @JsonProperty.value: name to use
    • @JsonProperty.index: physical index to use, if dataformat (other than JSON) is index-based
    • @JsonProperty.defaultValue: textual default value defined as metadata. NOTE: core databind does NOT make any use of this value; it is currently only exposed to extension modules.

Property Inclusion

  • @JsonAutoDetect: class annotation used for overriding property introspection definitions
  • @JsonIgnore: simple annotation to use for ignoring specified properties
  • @JsonIgnoreProperties: per-class annotation to list properties to ignore, or to indicate that any unknown properties are to be ignored.@JsonIgnoreType: per-class annotation to indicate that all properties of annotated type are to be ignored.
    • On serialization, @JsonIgnoreProperties({"prop1", "prop2"}) ignores listed properties
    • On deserialization, @JsonIgnoreProperties(ignoreUnknown=true) ignores properties that don't have getter/setters
  • @JsonInclude: annotation used to define if certain "non-values" (nulls or empty values) should not be included when serializing; can be used on per-property basis as well as default for a class (to be used for all properties of a class)

Deserialization and Serialization details

  • @JsonFormat: general annotation that has per-type behavior; can be used for example to specify format to use when serializing Date/Time values.

 

猜你喜欢

转载自simon-9527.iteye.com/blog/2332396