Springboot websockets 无法建立连接java.lang.NoSuchMethodError

Springboot websockets unable to establish connection java.lang.NoSuchMethodError

我正在尝试在我的应用程序中使用 websockets 来提供一对一的私人消息传递。在我的实施过程中,我一直在关注 this 项目。通过我在 angular 中的前端项目,我能够与作者 github 的克隆聊天服务建立连接,但是当我尝试连接到我自己的时,我收到此错误:

2020-11-04 19:21:53.918 ERROR 21648 --- [nio-7000-exec-9] o.s.w.s.m.StompSubProtocolHandler        : Failed to parse TextMessage payload=[CONNECT
ac..], byteCount=56, last=true] in session 0vmhpgm0. Sending STOMP ERROR to client.

java.lang.NoSuchMethodError: 'java.lang.String org.springframework.util.StreamUtils.copyToString(java.io.ByteArrayOutputStream, java.nio.charset.Charset)'
    at org.springframework.messaging.simp.stomp.StompDecoder.readCommand(StompDecoder.java:220) ~[spring-messaging-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    at org.springframework.messaging.simp.stomp.StompDecoder.decodeMessage(StompDecoder.java:143) ~[spring-messaging-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    at org.springframework.messaging.simp.stomp.StompDecoder.decode(StompDecoder.java:115) ~[spring-messaging-5.2.10.RELEASE.jar:5.2.10.RELEASE]

我的项目和另一个项目之间的唯一区别是我使用的是 postgres,所以我的回购和服务不同,但是在错误之前从未到达此代码,因为我在各处放置了断点并且 none 命中。 我无法弄清楚这个错误的真正含义,因为它非常模糊,没有指向任何特定方向。

我的配置:


@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/user");
        config.setApplicationDestinationPrefixes("/app");
        config.setUserDestinationPrefix("/user");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry
                .addEndpoint("/ws")
                .setAllowedOrigins("*")
                .withSockJS();
    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
        resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setObjectMapper(new ObjectMapper());
        converter.setContentTypeResolver(resolver);
        messageConverters.add(converter);
        return false;
    }
}

聊天控制器:


@Controller
public class ChatController {

    @Autowired private SimpMessagingTemplate messagingTemplate;
    @Autowired private ChatMessageService chatMessageService;
    @Autowired private ChatRoomService chatRoomService;

    @MessageMapping("/chat")
    public void processMessage(@Payload ChatMessage chatMessage) {
        var chatId = chatRoomService
                .getChatId(chatMessage.getSenderId(), chatMessage.getRecipientId(), true);
        chatMessage.setChatId(chatId.get());

        ChatMessage saved = chatMessageService.save(chatMessage);
        messagingTemplate.convertAndSendToUser(
                chatMessage.getRecipientId().toString(),"/queue/messages",
                new ChatNotification(
                        saved.getId(),
                        saved.getSenderId(),
                        saved.getSenderName()));
    }

    @GetMapping("/messages/{senderId}/{recipientId}/count")
    public ResponseEntity<Long> countNewMessages(
            @PathVariable UUID senderId,
            @PathVariable UUID recipientId) {

        return ResponseEntity
                .ok(chatMessageService.countNewMessages(senderId, recipientId));
    }

    @GetMapping("/messages/{senderId}/{recipientId}")
    public ResponseEntity<?> findChatMessages ( @PathVariable UUID senderId,
                                                @PathVariable UUID recipientId) {
        return ResponseEntity
                .ok(chatMessageService.findChatMessages(senderId, recipientId));
    }

    @GetMapping("/messages/{id}")
    public ResponseEntity<?> findMessage ( @PathVariable UUID id) {
        return ResponseEntity
                .ok(chatMessageService.findById(id));
    }
}

正如我所说,我的前端应该没有问题,因为它正在与另一个后端连接,但为了确保这是我的 angular 片段:

import * as Stomp from 'stompjs';
import * as SockJS from 'sockjs-client';
// ...

  connect() {
    const sockJS = new SockJS(API_URL + 'ws');
    this.stompClient = Stomp.over(sockJS);
    this.stompClient.connect({}, this.onConnected, this.onError);
  }

我很乐意得到一些帮助,因为我什至不知道什么是 broken/missing,这是我第一次使用 websockets,我在网上找不到类似的问题。

编辑: 根据要求添加了 pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>pl.edu.pjatk.mroz</groupId>
    <artifactId>project-API</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project-API</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>

        <!--SPRING-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.3.1.RELEASE</version>
        </dependency>

        <!--SPRINGBOOT-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.6.RELEASE</version>

        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>com.corundumstudio.socketio</groupId>
            <artifactId>netty-socketio</artifactId>
            <version>1.7.18</version>
        </dependency>



        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.12</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mindrot</groupId>
            <artifactId>jbcrypt</artifactId>
            <version>0.4</version>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <!-- API, java.xml.bind module -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
        </dependency>

        <!-- Runtime, com.sun.xml.bind module -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.885</version>
        </dependency>


        <!--testing-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.2.6.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <version>5.3.1.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>rental</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.4.RELEASE</version>
            </plugin>
        </plugins>
    </build>

</project>

由于您使用的是 spring 引导,因此您需要对所有工件使用相同的版本。

例如,在 pom.xml 中,您应该有类似这些行的内容:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

确保您使用的是相同版本的spring引导依赖项。

添加 pom.xml 后进行编辑:

以防万一,请在此处尝试 spring 初始值设定项:https://start.spring.io/ 以确保您拥有所有正确的版本。将生成的 pom.xml 与您的进行比较。

您似乎在使用不同的 spring 版本时弄乱了依赖项。

这个问题的罪魁祸首是-

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

由于这种依赖性,您有 spring-core-5.2.5.RELEASE

日志显示您正在使用 spring-messaging-5.2.10.RELEASE 使用 StreamUtils copyToString 方法的版本,该方法是在 5.2.6 版本中添加的。

如果您确保使用 spring-core-5.2.10.RELEASE 版本,应该可以解决问题。

您应该使用 pom.xml 的属性部分。为 Spring 引导版本添加新的 属性。请注意 spring 引导 2.3.1 将拉取 spring 5.2.10.RELEASE 版本。

<properties>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>

        <spring.boot.version>2.3.1.RELEASE</spring.boot.version>
</properties>

然后在 -

等依赖项中使用它
<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>

您应该对所有 spring 引导启动程序依赖项执行类似的操作。另外删除那些显式 spring 依赖项,例如 spring-context.

我 运行 遇到了同样的问题,但是无法移动到较新的 spring 引导版本。我找到的解决方案是将 spring-messaging 版本降级到 5.2.5.RELEASE(spring-websocket 也是如此)并且它有效。