SimpMessagingTemplate does not send messages to spring boot

Hi everyone, I'm trying to send messages to Stomp Endpoints, but I don't get any. I use spring boot with stomp, the following are my classes

@Controller
public class GreetingController {

  @MessageMapping("/hello")
  @SendTo("/topic/greetings")
  public Greeting greeting(HelloMessage message) throws Exception {
    System.out.println(message.getName());
    Thread.sleep(13000); // simulated delay
    return new Greeting("Hello, " + message.getName() + "!");
  }

}

@Controller                                                
public class Testcont {

  @Autowired
  private SimpMessagingTemplate messageSender;

  @RequestMapping(value="/Users/get",method=RequestMethod.POST)
  @ResponseBody
  public String getUser(@RequestParam(value = "userId") String userId, @RequestParam(value = "password") String password, @RequestParam(value="port") String port, HttpServletRequest request) {
    HelloMessage mess=new HelloMessage();
    mess.setName(userId);
    messageSender.convertAndSend("/app/hello",mess);
    return "Success";

}

and my configuration for websocket

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

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

}

I do not get any errors in the consoles. This code works great with web browsers.

+4
source share
1 answer

SimpMessagingTemplateThe bean is designed specifically for the Broker ( AbstractMessageBrokerConfiguration) part:

@Bean
public SimpMessagingTemplate brokerMessagingTemplate() {
    SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel());
    String prefix = getBrokerRegistry().getUserDestinationPrefix();
    if (prefix != null) {
        template.setUserDestinationPrefix(prefix);
    }

Since you are not sending the message to the broker's addressee ( /app/in your case), such a message is simply ignored AbstractBrokerMessageHandler.checkDestinationPrefix(destination).

@MessageMapping, clientInboundChannel , SimpAnnotationMethodMessageHandler:

@Bean
public SimpAnnotationMethodMessageHandler simpAnnotationMethodMessageHandler() {
    SimpAnnotationMethodMessageHandler handler = createAnnotationMethodMessageHandler();
    handler.setDestinationPrefixes(getBrokerRegistry().getApplicationDestinationPrefixes());

, SimpMessagingTemplate clientInboundChannel, brokerMessagingTemplate bean. .

+4

All Articles