gpt4 book ai didi

java - SockJsClient 抛出 ClassCastException

转载 作者:行者123 更新时间:2023-12-01 22:27:22 27 4
gpt4 key购买 nike

我正在尝试在我的 intellij IDEA 插件中配置 WebSocket 连接,我得到了

 2019-10-25 19:17:56,835 [  81159]  ERROR - roject.impl.ProjectManagerImpl - From the listener com.homework.plugin.PluginTasksProjectComponent$1@1c0e28b2 (class com.homework.plugin.PluginTasksProjectComponent$1) 
java.lang.ClassCastException: com.fasterxml.jackson.module.kotlin.KotlinModule cannot be cast to com.fasterxml.jackson.databind.Module
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.registerWellKnownModulesIfAvailable(Jackson2ObjectMapperBuilder.java:811)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.configure(Jackson2ObjectMapperBuilder.java:650)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:633)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.<init>(MappingJackson2HttpMessageConverter.java:59)
at org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.<init>(AllEncompassingFormHttpMessageConverter.java:76)
at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:150)
at org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport.<init>(RestTemplateXhrTransport.java:64)
at com.homework.plugin.chat.WebSocket.<init>(WebSocket.java:39)
at com.homework.plugin.PluginTasksProjectComponent$1.projectOpened(PluginTasksProjectComponent.java:72)

当我在独立应用程序中使用此代码时,一切都很完美。

public class WebSocket {
private static final String TOPIC_ADDED = "/topic/discussion%s/comments/added";
private static final String TOPIC_UPDATED = "/topic/discussion%s/comments/updated";
private static final String TOPIC_DELETED = "/topic/discussion%s/comments/deleted";
private static final String TOPIC_SEND = "/topic/discussion%s/comments";
private final String URL = "https://my.url/stomp-messages";

private StompSession stompSession;
private StompSessionHandler sessionHandler;
private StompSession.Subscription currentSubscription;


public WebSocket(JTextPane messagesTextPane) {
sessionHandler = new MySessionHandler(messagesTextPane);
List<Transport> transports = new ArrayList<>(2);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
transports.add(new RestTemplateXhrTransport());
WebSocketClient client = new SockJsClient(transports);

WebSocketStompClient stompClient = new WebSocketStompClient(client);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
stompClient.setTaskScheduler(new ConcurrentTaskScheduler());

try {
stompSession = stompClient.connect(URL, sessionHandler).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}

在创建 new RestTemplateXhrTransport() 对象的行抛出异常。 Jackson2ObjectMapperBuilder 尝试将一些 Kotlin 模块转换为 Module 类,但我无法理解为什么这段代码不在独立应用程序中执行。

可能有人知道问题的根源吗?

最佳答案

问题已解决。我应该添加这个依赖项

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.10.0</version>
</dependency>

关于java - SockJsClient 抛出 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58562526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com