springboot jackson实现自定义日期格式

背景

  • 如今炒的火热的前后端分离项目,大多数开发人员选择RESTful设计风格,Java Web 人员经常要设计 RESTfulAPI,这种设计通常使用 json 数据进行交互。那么前端传入的 json 数据如何序列化成 Java 对象,后端返回的结果又如何将Java 对象解析成 json 格式数据返回给前端。
  • spring-boot-stater-web依赖的json解析是jackson,同时也为我们进行了jackson的一系列自动化配置,这样我们不需要导入其他json依赖,就可以直接使用,其中起到关键作用的是
    MappingJackson2HttpMessageConverter

问题

Caused by:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of java.time.ZonedDateTime (no Creators, like
default construct, exist): no String-argument constructor/factory
method to deserialize from String value
(‘2018-05-26T09:48:31.622+02:00’)

什么意思呢?

就是说在反序列化字符串(‘2018-05-26T09:48:31.622+02:00’)时,没有找到String类型的构造方法,无法构建出java.time.ZonedDateTime类型对应的实例,抛出了InvalidDefinitionException不合法定义的异常,那么说明框架本身不能解析这种时间格式,需要我们自己去解析

解决方案

方案一:采用jackson的@JsonDeserialize注解

@JsonSerialize(using = CustomZonedTimeSerialize.class)
@JsonDeserialize(using = CustomZonedTimeDeserialize.class)
private ZonedDateTime birth;

这里我们可以根据自己系统的前端架构中时间格式做调整,这里兼容
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd’T’HH:mm:ss.SSS’Z’
yyyy/MM/dd HH:mm:ss
yyyy-MM-dd’T’HH:mm:ss.SSSXXX

这些时间格式

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 时间    反序列化自定义
 *
 *
 *  兼容以下字符格式的时间串,正确转为ZonedDateTime类型实例
 */
public class CustomZonedTimeDeserialize extends JsonDeserializer<ZonedDateTime> {

    private DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
    private DateTimeFormatter sdf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneId.systemDefault());
    private DateTimeFormatter sdf3 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());
    private DateTimeFormatter sdf4 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withZone(ZoneId.systemDefault());


    public CustomZonedTimeDeserialize() {
    }

    @Override
    public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        ZonedDateTime dateTime = null;
        if (StringUtils.isNotBlank(p.getText())) {
            if (StringUtils.isNotBlank(p.getText())) {
                if (p.getText().indexOf("T") > 0 && p.getText().indexOf("Z") > 0) {
                    dateTime = ZonedDateTime.parse(p.getText(),sdf2);
                }else if (p.getText().indexOf("/") > 0) {
                    dateTime = ZonedDateTime.parse(p.getText(),sdf3);
                } else if (p.getText().indexOf("+") > 0) {
                    dateTime = ZonedDateTime.parse(p.getText(),sdf4);
                } else {
                    dateTime = ZonedDateTime.parse(p.getText(),sdf);
                }
            }
        }

        return dateTime;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot中,可以通过自定义Jackson实现定义序列化和反序列化的需求。以下是一个简单的示例,演示了如何自定义Jackson。 首先,我们需要创建一个Jackson的ObjectMapper Bean,并指定我们自定义的序列化器和反序列化器。示例中,我们自定义了一个格式日期的序列化器和反序列化器。 ```java @Configuration public class JacksonConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(Date.class, new CustomDateSerializer()); module.addDeserializer(Date.class, new CustomDateDeserializer()); objectMapper.registerModule(module); return objectMapper; } } ``` 然后,我们需要实现定义的序列化器和反序列化器。示例中,我们自定义了一个格式日期的序列化器和反序列化器。 ```java public class CustomDateSerializer extends JsonSerializer<Date> { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(dateFormat.format(value)); } } public class CustomDateDeserializer extends JsonDeserializer<Date> { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { String dateValue = p.getText(); try { return dateFormat.parse(dateValue); } catch (ParseException e) { throw new RuntimeException(e); } } } ``` 现在,我们已经完成了自定义Jackson的配置。在使用时,我们可以直接使用@Autowired注解注入ObjectMapper Bean,或者使用@JsonComponent注解来标识我们的自定义序列化器和反序列化器。 ```java @RestController public class UserController { @Autowired private ObjectMapper objectMapper; @PostMapping("/user") public User addUser(@RequestBody User user) throws JsonProcessingException { String json = objectMapper.writeValueAsString(user); System.out.println(json); return user; } } @JsonComponent public class DateJsonComponent { public static class Serializer extends JsonSerializer<Date> { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(dateFormat.format(value)); } } public static class Deserializer extends JsonDeserializer<Date> { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { String dateValue = p.getText(); try { return dateFormat.parse(dateValue); } catch (ParseException e) { throw new RuntimeException(e); } } } } ``` 以上就是自定义Jackson的简单示例。通过自定义Jackson,我们可以轻松实现定义的序列化和反序列化需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大佬腿好粗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值