Skip to content

[Bug] Java 8 date/time type java.time.ZonedDateTime not supported by default #588

@woxiangbo

Description

@woxiangbo

Search before asking

  • I have searched the existing issues before asking.

AREX Test Service

AREX Java Agent (arextest/arex-agent-java)

Current Behavior

在录制的时候,agent报如下错误:

[[title=arex.serializer-serialize]]cccc-->can not serialize object: org.springframework.http.ResponseEntity-com.tencent.gms.gms.web.rest.vm.usermanagement.SimpleUserVM, serializer: null, cause: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.ZonedDateTime not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: org.springframework.http.ResponseEntity["body"]->com.tencent.gms.web.rest.RespResult["timestamp"])

Expected Behavior

能够正常序列化

Steps To Reproduce

在arex-agent 的源码(版本0.4.6)中新建一个类,代码参考如下:

package io.arex.integrationtest.mainapp;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.arex.foundation.serializer.jackson.ArexObjectMapper;

import java.time.ZonedDateTime;

public class MainApp {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper MAPPER = new ArexObjectMapper();
        

        User user = new User();
        String s =  MAPPER.writeValueAsString(user);
        System.out.println(s);
    }
}

class User{
    private String userName;
    private ZonedDateTime timestamp = ZonedDateTime.now();

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public ZonedDateTime getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(ZonedDateTime timestamp) {
        this.timestamp = timestamp;
    }
}

执行这个main方法就会报错

怀疑bug发生的地方: https://github.com/arextest/arex-agent-java/blob/main/arex-instrumentation-foundation/src/main/java/io/arex/foundation/serializer/jackson/JacksonSerializer.java 第88行
需要添加

MAPPER.registerModule(new JavaTimeModule());

这个JavaTimeModule 来自于com.fasterxml.jackson.datatype.jsr310
依赖大概长这样:

<dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.17.1</version>
    </dependency>

Anything else

No response

Are you willing to submit a pull request to fix on your own?

  • Yes I am willing to submit a pull request on my own!

Metadata

Metadata

Assignees

Labels

bug 🐞Something isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions