温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Jackson的基础核心用法有哪些

发布时间:2021-12-13 19:12:12 来源:亿速云 阅读:160 作者:柒染 栏目:大数据

本篇文章为大家展示了Jackson的基础核心用法有哪些,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

Jackson是Spring Boot默认的JSON数据处理框架,但是其并不依赖于任何的Spring 库。有的小伙伴以为Jackson只能在Spring框架内使用,其实不是的,没有这种限制。它提供了很多的JSON数据处理方法、注解,也包括流式API、树模型、数据绑定,以及复杂数据类型转换等功能。它虽然简单易用,但绝对不是小玩具,下面为大家介绍Jackson的基础核心用法

一、从URL读取JSON数据

Jackson不仅可以将字符串反序列化为 Java POJO对象,还可以请求远程的API,获得远程服务的JSON响应结果,并将其转换为Java POJO对象。

[@Test](https://my.oschina.net/azibug)
void testURL() throws IOException {

  URL url = new URL("https://jsonplaceholder.typicode.com/posts/1"); //远程服务URL
  ObjectMapper mapper = new ObjectMapper();
  //从URL获取JSON响应数据,并反序列化为java 对象
  PostDTO postDTO = mapper.readValue(url, PostDTO.class); 

  System.out.println(postDTO);

}
  • jsonplaceholder.typicode.com 是一个免费提供HTTP测试服务的网站,我们可以利用它进行测试

  • 远程服务API返回结果是一个JSON字符串,一篇post稿件包含userId,id,title,content属性

  • PostDTO 是我们自己定义的java 类,同样包含userId,id,title,content成员变量

下文是控制台打印输出结果,postDTO的toString()方法输出。

PostDTO(userId=1, id=1, title=sunt aut facere repellat provident occaecati excepturi optio reprehenderit, body=quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto)

二、Unknow Properties 赋值失败处理

有的时候,客户端提供的JSON字符串属性,多于我们服务端定义的java 类的成员变量。

Jackson的基础核心用法有哪些

比如上图中的两个类,

  • 我们先将PlayerStar序列化为JSON字符串,包含age属性

  • 然后将JSON字符串转换为PlayerStar2,不包含age属性

[@Test](https://my.oschina.net/azibug)
void testUnknowProperties() throws IOException {
  ObjectMapper mapper = new ObjectMapper();
  PlayerStar player = PlayerStar.getInstance(); //为PlayerStar 各属性赋值,可以参考本系列文章第一篇

  //将PlayerStar序列化为JSON字符串
  String jsonString = mapper.writeValueAsString(player);
  System.out.println(jsonString);
  //将JSON字符串反序列化为PlayerStar2对象
  PlayerStar2 player2 = mapper.readValue(jsonString, PlayerStar2.class);
  System.out.println(player2);
}

当进行反序列化的时候,会抛出下面的异常。这是因为JSON字符串所包含的属性,多余Java类的定义(多出一个阿age,赋值时找不到setAge方法)。

{"age":45,"playerName":"乔丹"}

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "age" (class com.example.demo.javabase.PlayerStar2), not marked as ignorable (one known property: "playerName"])
 at [Source: (String)"{"age":45,"playerName":"乔丹"}"; line: 1, column: 10] (through reference chain: com.example.demo.javabase.PlayerStar2["age"])

如果我们想忽略掉age属性,不接受我们的java类未定义的成员变量数据,可以使用下面的方法,就不会抛出UnrecognizedPropertyException了。

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

三、未赋值Java Bean序列化

有的时候,我们明知道某些类的数据可能为空,我们通常也不会为它赋值。但是客户端就是需要这个{}的JSON对象,我们该怎么做?

public class MyEmptyObject {
  private Integer i;  //没有get set方法
}

我们可以为ObjectMapper设置disable序列化特性:FAIL_ON_EMPTY_BEANS,也就是允许对象的所有属性均未赋值。

[@Test](https://my.oschina.net/azibug)
void testEmpty() throws IOException {

  ObjectMapper mapper = new ObjectMapper();
  mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

  String jsonString = mapper.writeValueAsString(new MyEmptyObject());
  System.out.println(jsonString);

}

默认情况下不做设置,会抛出下面的异常InvalidDefinitionException。设置disable序列化特性:FAIL_ON_EMPTY_BEANS之后,会序列化为{}字符串。

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.example.demo.jackson.JacksonTest1$MyEmptyObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

四、日期格式化

日期格式化,是我们JSON序列化与反序列化过程中比较常见的需求

ObjectMapper mapper = new ObjectMapper();
Map temp = new HashMap();
temp.put("now", new Date());
String s = mapper.writeValueAsString(temp);
System.out.println(s);

默认情况下,针对java中的日期及相关类型,Jackson的序列化结果如下

{"now":1600564582571}

如果我们希望在JSON序列化及反序列化过程中,日期格式化,需要做如下的处理

ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);  //注意这里
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));  //注意这里
Map temp = new HashMap();
temp.put("now", new Date());

String s = mapper.writeValueAsString(temp);
System.out.println(s);

控制台打印输出结果如下:

{"now":"2020-09-20"}

上述内容就是Jackson的基础核心用法有哪些,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI