jackson在实际应用中给我们提供了一系列注解,提高了开发的灵活性,下面介绍一下最常用的一些注解

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

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

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

@JsonSerialize
此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点,将一个Date类型转化成指定类型字符串。

  1. public class JsonDoubleSerialize extends JsonSerializer<Double> {
  2.  
  3. private DecimalFormat df = new DecimalFormat("##.000");
  4.  
  5. @Override
  6. public void serialize(Double value, JsonGenerator jgen,
  7. SerializerProvider provider) throws IOException,
  8. JsonProcessingException {
  9.  
  10. jgen.writeString(df.format(value));
  11. }
  12. }
  1. /**
  2. * 把Date类型序列化成指定合适的字符串
  3. */
  4. public class JsonDateSerialize extends JsonSerializer<Date> {
  5. @Override
  6. public void serialize(Date date, JsonGenerator jgen,
  7. SerializerProvider provider)
  8. throws IOException, JsonProcessingException {
  9. String formattedDate = "";
  10. if (date != null) {
  11. //把日期序列化成yyyy-MM-dd格式的字符串
  12. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  13. formattedDate = simpleDateFormat.format(date);
  14. }
  15. jgen.writeString(formattedDate);
  16. }
  17. }

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

  1. /**
  2. * 将一个字符串反序列化成一个Date类型
  3. */
  4. public class JsonDateDeserialize extends JsonDeserializer<Date> {
  5.  
  6. @Override
  7. public Date deserialize(JsonParser jp, DeserializationContext ctxt)
  8. throws IOException, JsonProcessingException {
  9. //拿到的是"yyyy-MM-dd"形式的字符串,现在要在json反序列化的时候转化成Date类型
  10. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  11. String date = jp.getText();
  12. if (date == null || date.trim().length() == 0) {
  13. return null;
  14. }
  15. try {
  16. return format.parse(date);
  17. } catch (Exception e) {
  18.  
  19. }
  20. return null;
  21. }
  22. }

完整例子

  1. //表示序列化时忽略的属性
  2. @JsonIgnoreProperties(value = {"word"})
  3. public class Person {
  4. private String name;
  5. private int age;
  6. private boolean sex;
  7. @JsonSerialize(using = JsonDateSerialize.class)
  8. @JsonDeserialize(using = JsonDateDeserialize.class)
  9. private Date birthday;
  10. private String word;
  11. @JsonSerialize(using = JsonDoubleSerialize.class)
  12. private double salary;
  13.  
  14. public String getName() {
  15. return name;
  16. }
  17.  
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21.  
  22. public int getAge() {
  23. return age;
  24. }
  25.  
  26. public void setAge(int age) {
  27. this.age = age;
  28. }
  29.  
  30. public boolean isSex() {
  31. return sex;
  32. }
  33.  
  34. public void setSex(boolean sex) {
  35. this.sex = sex;
  36. }
  37.  
  38. public Date getBirthday() {
  39. return birthday;
  40. }
  41.  
  42. public void setBirthday(Date birthday) {
  43. this.birthday = birthday;
  44. }
  45.  
  46. public String getWord() {
  47. return word;
  48. }
  49.  
  50. public void setWord(String word) {
  51. this.word = word;
  52. }
  53.  
  54. public double getSalary() {
  55. return salary;
  56. }
  57.  
  58. public void setSalary(double salary) {
  59. this.salary = salary;
  60. }
  61.  
  62. public Person(String name, int age) {
  63. this.name = name;
  64. this.age = age;
  65. }
  66.  
  67. public Person(String name, int age, boolean sex, Date birthday,
  68. String word, double salary) {
  69. super();
  70. this.name = name;
  71. this.age = age;
  72. this.sex = sex;
  73. this.birthday = birthday;
  74. this.word = word;
  75. this.salary = salary;
  76. }
  77.  
  78. public Person() {
  79. }
  80.  
  81. @Override
  82. public String toString() {
  83. return "Person [name=" + name + ", age=" + age + ", sex=" + sex
  84. + ", birthday=" + birthday + ", word=" + word + ", salary="
  85. + salary + "]";
  86. }
  87.  
  88. }
  1. public class Demo {
  2. public static void main(String[] args) {
  3.  
  4. //writeJsonObject();
  5.  
  6. readJsonObject();
  7. }
  8.  
  9. // 直接写入一个对象(所谓序列化)
  10. public static void writeJsonObject() {
  11. ObjectMapper mapper = new ObjectMapper();
  12. Person person = new Person("zhangsan", 25, true, new Date(), "coder",
  13. 2500.0);
  14. try {
  15. String string = mapper.writeValueAsString(person);
  16. //{"name":"zhangsan","age":25,"sex":true,"birthday":"2016-12-03 22:02:23","salary":"2500.000"}
  17. System.out.println(string);
  18. } catch (JsonGenerationException e) {
  19. e.printStackTrace();
  20. } catch (JsonMappingException e) {
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26.  
  27. // 直接将一个json转化为对象(所谓反序列化)
  28. public static void readJsonObject() {
  29. ObjectMapper mapper = new ObjectMapper();
  30.  
  31. try {
  32. String string = "{\"name\":\"zhangsan\",\"age\":25,\"sex\":true,\"birthday\":\"2016-12-03 22:02:23\",\"word\":\"coder\",\"salary\":\"2500.000\"}";
  33. Person person = mapper.readValue(string, Person.class);
  34. //Person [name=zhangsan, age=25, sex=true, birthday=Sat Dec 03 00:00:00 CST 2016, word=null, salary=2500.0]
  35. System.out.println(person.toString());
  36. } catch (JsonParseException e) {
  37. e.printStackTrace();
  38. } catch (JsonMappingException e) {
  39. e.printStackTrace();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }

Jackson(使用注解)的更多相关文章

  1. jackson annotations注解详解

    jackson中常用到的注解 猛击下面的连接地址 http://blog.csdn.net/sdyy321/article/details/40298081

  2. jackson annotations注解详解 (zhuan)

    http://blog.csdn.net/sdyy321/article/details/40298081 ************************************** 官方WIKI: ...

  3. jackson基于注解的简单使用

    Jackson提供了一系列注解,方便对JSON序列化和反序列化进行控制,下面介绍一些常用的注解. 1.@JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性. 2.@JsonFo ...

  4. jackson 常用注解,比如忽略某些属性,驼峰和下划线互转

    一般情况下使用JSON只使用了java对象与字符串的转换,但是,开发APP时候,我们经常使用实体类来做转换:这样,就需要用到注解: Jackson默认是针对get方法来生成JSON字符串的,可以使用注 ...

  5. Jackson /常用注解/ annotation(转)

    1.@JsonAutoDetect 自动检测,(作用在类上)来开启/禁止自动检测. fieldVisibility:字段的可见级别 ANY:任何级别的字段都可以自动识别 NONE:所有字段都不可以自动 ...

  6. Jackson常用注解及用法

    最近写项目,用到Jackson的一些注解,总结一下,帮助自己记忆. 1.@JsonIgnore 和 @JsonIgnoreProperties 两个注解可以对照比较后选择使用: @JsonIgnore ...

  7. json序列化反序列化Jackson相关注解

    1.@Transient @Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性:如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则ORM框架 ...

  8. jackson JsonPropertyOrder和@JsonIgnoreProperties注解

    有些时候,我们在和外部系统交互的时候使用了json作为标准的数据交换格式,同时为了安全性考虑,增加了对报文的校验,因此我们需要确保序列化的时候参数有序且不多不少刚好,因为对外的API不像后台和前端交互 ...

  9. jackSon注解– @JsonInclude 注解不返回null值字段

    @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO { private String orderId; @Js ...

  10. Jackson中@JsonProperty等常用注解

    Java生态圈中有很多处理JSON和XML格式化的类库,Jackson是其中比较著名的一个.虽然JDK自带了XML处理类库,但是相对来说比较低级 本文将介绍的Jackson常用注解:精简概述 Jack ...

随机推荐

  1. mysql如何分类统计数量

    比如我表test里面有id,mc,xh三个字段(分别是自动编号,钢材名称(若干种),钢材型号(大号,中号,小号)) id mc xh 钢管 大号 铜管 大号 铁管 小号 铝管 中号 钢管 小号 我现在 ...

  2. MySQL 源码系列:1:窥探篇

    1:下载源码 http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.25.tar.gz http://dev.mysql.com/downloads/m ...

  3. C3P0连接池使用教程

     转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6405861.html  在项目中的应用见: https://github.com/ygj0930/Coupl ...

  4. cocos2d-js 3.0 rc0 编译release报错 value for keystore is not valid. it must resolve to a single path

    第一次编译是好好的,需要手工输入keystore文件地址和密码等等.第二次不需要输入,然后就直接出错了.   找了一下,发现第一步之后,cocos会记录ant信息到\frameworks\runtim ...

  5. Java 实现的SnowFlake生成UUID (Java代码实战-007)

    SnowFlake所生成的ID一共分成四部分: 1.第一位占用1bit,其值始终是0,没有实际作用. 2.时间戳占用41bit,精确到毫秒,总共可以容纳约69 年的时间. 3.工作机器id占用10bi ...

  6. 〖Linux〗git push orgin master不能解析域名的解决方法

    错误信息: $ git push origin master ssh: Could not resolve hostname bitbucket.org: Name or service not kn ...

  7. VTK中的装配体(vtkAssembly)

    Actors有时也会组合在一起形成层次结构,当其中的某个Actor运动时,会影响到其他Actor的位置.例如,一个机械手臂可能由上臂.前臂.手腕和末端等部分通过关节连接起来.当上臂绕着肩关节旋转时,我 ...

  8. linux下安装和卸载vmware产品

    1.安装 一般的发行版都不会带有vmware,所以通常是下载安装包来安装. 具体的可以见 http://www.cnblogs.com/oloroso/p/5845227.html 2.卸载 这里主要 ...

  9. 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean

    1.有时候在项目中需要定时启动某个任务,对于这个需求,基于JavaEE规范,我们可以使用Listener与TimerTask来实现,代码如下: public class TestTaskListene ...

  10. Mysql分页之limit用法与limit优化

    Mysql limit分页语句用法 与Oracle和MS SqlServer相比,mysql的分页方法简单的让人想哭. --语法: SELECT * FROM table LIMIT [offset, ...