首页 > 解决方案 > 创建事件时返回“java.lang.ClassCastException”

问题描述

我正在尝试使用 Microsoft Graph 创建一个事件。有时我会得到一个ClassCastException,但我不知道为什么在创建事件时返回“响应二进制”是什么情况?

我的代码是这样的:

ClientCredentialProvider authProvider = new GraphAuthProvider(accessToken);
IGraphServiceClient graphClient = GraphServiceClient
                .builder()
                .authenticationProvider(authProvider)
                .buildClient();

Event event = graphClient
                .users(USER)
                .events()
                .buildRequest(requestOptions)
                .post(event);

我收到了这个错误:

java.io.BufferedInputStream cannot be cast to com.microsoft.graph.models.extensions.Event"
java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to com.microsoft.graph.models.extensions.Event
        at com.microsoft.graph.requests.extensions.EventRequest.post(EventRequest.java:112)
        at com.microsoft.graph.requests.extensions.EventCollectionRequest.post(EventCollectionRequest.java:67)

何时graphClient.getLogger().setLoggingLevel(LoggerLevel.DEBUG);添加:

Starting to send request, URL https://graph.microsoft.com/v1.0/users/USER/events
com.microsoft.graph.logger.DefaultLogger logDebug
Request Method PATCH
com.microsoft.graph.logger.DefaultLogger logDebug
Sending com.microsoft.graph.models.extensions.Event as request body
com.microsoft.graph.logger.DefaultLogger logDebug
Serializing type Event
com.microsoft.graph.logger.DefaultLogger logDebug
Response code 200, OK
com.microsoft.graph.logger.DefaultLogger logDebug
Response binary
com.microsoft.graph.logger.DefaultLogger logDebug
Deserializing type Event

用SDK查看日志,我猜如果返回“Response binary”,就会出现上述错误。它只发生在远程计算机上,我无法在库中调试。创建事件时返回“响应二进制”是什么情况?

这是我正在使用的库。

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>msal4j</artifactId>
    <version>1.4.0</version>
</dependency>
<dependency>
    <groupId>com.microsoft.graph</groupId>
    <artifactId>microsoft-graph</artifactId>
    <version>1.7.0</version>
</dependency>

如果我的英语难以理解,我很抱歉。如果你给我答案会很有帮助。

标签: javasdkmicrosoft-graph-api

解决方案


我建议您只使用最新的构建/SDK,而不是旧的构建。即使它可能是错误或回归,也可能已在以后的版本中修复。这就是为什么我总是使用最新的 SDK/构建,看看你是否可以重现这个问题。很高兴您尝试了它并且它对您有用。


推荐阅读