Spring Boot (Jackson 介绍)
Spring boot JackSon
为什么会使用到Jackson,一般情况在日常工作中,有部分操作都是需要序列化数据的。那么使用SpringBoot 内置 JackSon 调用起来更方便。
1、首先写一个简单的参数方法
添加注解
@Data//实现get set方法
@NoArgsConstructor //无参的构造函数
@AllArgsConstructor // 全参的构造函数
@Builder
Jackson常用注解:
@JsonProperty("rt")
使用这个注解之后,原先定义的变量名序列化返回变量名称改变成rt
-----------------------------------------------------------
@JsonIgnore
或略下面字段,不返回任何数据
-----------------------------------------------------------
@JsonIgnoreProperties({"xx"......})
忽略一组属性
-----------------------------------------------------------
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
日期格式化
-----------------------------------------------------------
@Data//实现get set方法 @NoArgsConstructor //无参的构造函数 @AllArgsConstructor // 全参的构造函数 @Builder @JsonIgnoreProperties({"age"}) //忽略一组属性 public class caicaiUser { private String name; private int age; //@JsonIgnore//或略下面字段,不返回任何数据 private String address; @JsonProperty("rt") //使用这个注解之后,registerTime返回变量名称改变成rt //如果不使用这是方法,后面返回变量值还是registerTime。 //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")//日期格式化 private Date registerTime; }
3、自定义一个Jackson
编写配置类,必须要加入一个
@Configuration 注解
声明一个序列化操作对象 ObjectMapper
然后进行定制设计
package com.caicai.springboot.study.config; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.text.SimpleDateFormat; /* * 配置jackson 日期数据格式化 * * * */ @Configuration public class JacksonConfig { @Bean public ObjectMapper getObjectMapper(){ ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);//参数是空的不会再序列化数据的时候返回 mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); return mapper; } }
4、测试自定义jackson定义类
Jackson 涉及序列化和反序列操作,所以首先使用
@Autowired
注入序列化操作对象Objectmapper
//ObjectMapper注入进来
private ObjectMapper mapper;//用于序列化和反序列化操作
package com.caicai.springboot.study.controller; import com.caicai.springboot.study.config.getnameversionconfig; import com.caicai.springboot.study.vo.caicaiUser; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /* * Test controller * * */ @Slf4j @RestController //序列化数据,反序列化数据 @RequestMapping("/Springboot") public class insert_controller { @Autowired //ObjectMapper注入进来 private ObjectMapper mapper;//用于序列化和反序列化操作 @GetMapping("/conf_jsckson") public caicaiUser jackson() throws Exception{ // caicaiUser user = caicaiUser.builder() //new 一个对象 // .address("幸福路1号") // .age(11) // .name("caicai") // .registerTime( new Date()) // .build(); caicaiUser user = new caicaiUser(); user.setAddress("幸福路1号"); user.setAge(22); user.setName("caicai"); user.setRegisterTime(new Date()); String jsoncaiciauser = mapper.writeValueAsString(user);//将user对象序列化为json格式字符串 log.info("caiciajasckson {}",jsoncaiciauser); //反序列操作:使用ObjectMapper 中的readValue方法来反序列化读取数据 return mapper.readValue(jsoncaiciauser,caicaiUser.class); } }
解释一下为什么 上面会使用 .xxx来配置参数。因为我在编写caicaiUser类的时候我使用了 @Builder 这个注解。 在声明对象的时候直接 对象.Builder() 然后就可以直接使用 . xxx
开开心心,上班!
快快乐乐,游玩!
及时行乐!