SockJsClient抛出ClassCastException

SockJsClient抛出ClassCastException

我正在尝试在我的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/

10-10 14:34