为什么我使用默认值@PostMapping时无法连接到websocket?

时间:2019-10-29 19:23:03

标签: java spring-boot spring-websocket

当我在Controller中没有值的@PostMapping我无法连接到WebSocket时,我收到405方法不允许,但是如果我在@PostMapping(“ / example”)中输入值,那么我可以连接到WebSocket并且一切正常。这是所需的动作吗?

我的Spring Boot版本是2.1.9.RELEASE。

非常感谢您的帮助

@RestController("/api")
public class ExampleResource {

    @PostMapping
    public String getExample(String example) {
        return example;
    }
}
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").setAllowedOrigins("*");
        registry.addEndpoint("/ws")
                .setAllowedOrigins("*")
                .setHandshakeHandler(new DefaultHandshakeHandler(new TomcatRequestUpgradeStrategy()))
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config
                .setApplicationDestinationPrefixes("/app", "/queue")
                .enableStompBrokerRelay("/topic")
                .setRelayHost("localhost")
                .setRelayPort(61613)
                .setClientLogin("rabbitmq")
                .setClientPasscode("rabbitmq")
                .setSystemLogin("rabbitmq")
                .setSystemPasscode("rabbitmq");
    }
}

0 个答案:

没有答案