Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
woxiangbo opened this issue Nov 27, 2024 · 1 comment
Open
2 tasks done
Assignees
Labels
bug 🐞 Something isn't working

Comments

@woxiangbo
Copy link

woxiangbo commented Nov 27, 2024

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!
@woxiangbo woxiangbo added the bug 🐞 Something isn't working label Nov 27, 2024
@lucas-myx
Copy link
Collaborator

不需要引入而外的jsr依赖,参考io.arex.foundation.serializer.jackson.adapter包下的做法,添加一个java.time.ZonedDateTime的适配器即可,期待你的pr

@lucas-myx lucas-myx self-assigned this Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants