I write a chat on the web socket. I use sockjs.js and stomp.js. The connection is successful, subscriber to the newsletter, too. But when you try to send a message to the backend (for sending to all those who joined the chat), nothing comes to the desired controller, although it writes to the console that it sent the data:

>>> SEND destination:/app/app-dest-prefix/chat content-length:76 {"from":"Lesha","text":"message","timeCreation":"2017-10-12T15:26:29.410Z"} 

so i join

 stompClient = Stomp.over(new SockJS("/ContactBook/app/cbxSoc")); 

so sabsibre:

 stompClient.subscribe('/topic/messages', function (message) { console.log(message); }); 

so i'm sending a message:

 stompClient.send("/app-dest-prefix/chat", {}, JSON.stringify({'from':'Lesha', 'text':$scope.chatInput, 'timeCreation':new Date()}) 

I have such a controller

 @Controller public class ChatController { @MessageMapping("/chat") @SendTo("/topic/messages") public Message send(Message message){ String time = new SimpleDateFormat("HH:mm").format(new Date()); message.setTimeCreation(time); return message; } 

}

such configuration:

 @Configuration @EnableWebSocketMessageBroker public class CbxSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{ @Override public void registerStompEndpoints(StompEndpointRegistry ser) { ser.addEndpoint("/cbxSoc").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry config) { // Prefix for messages FROM client TO server config.setApplicationDestinationPrefixes("/app-dest-prefix"); // Prefix for messages FROM server TO client config.enableSimpleBroker("/events", "/topic", "/queue"); config.setUserDestinationPrefix("/user"); } } 

Tell me who knows where I was wrong. Thank. It seems that the URL did not indicate the correct one, but judging by the documentation, everything is correct ...

UPD Maybe someone knows how to deny everything at all. What comes from the client (not only on a certain path, which is specified in @MessageMapping ("/ chat"))?

UPD2 Added a handler to catch everyone’s post on the webbox

  @Override public void configureClientInboundChannel(ChannelRegistration registration) { registration.setInterceptors(new MyChannelInterceptor()); } public class MyChannelInterceptor extends ChannelInterceptorAdapter { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); //StompCommand command = accessor.getStompCommand(); return message; } } 

When a client sends a message, this handler catches it, but it still does not reach the controller .. enter image description here

UPD3 example log mapping

 2017-10-22 20:30:19 INFO RequestMappingHandlerMapping:220 - Mapped "{[/client/managers],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public java.util.List com.miroktell.contactbook.controllers.ClientController.getManagers() 2017-10-22 20:30:19 INFO RequestMappingHandlerMapping:220 - Mapped "{[/client/filter],methods=[POST],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public com.miroktell.contactbook.containers.RowsContainer com.miroktell.contactbook.controllers.ClientController.getClientsByFilter(com.miroktell.contactbook.containers.ClientListFilter) throws java.io.IOException 2017-10-22 20:30:19 INFO RequestMappingHandlerMapping:220 - Mapped "{[/client/getFullClientByPhoneNumber],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public com.miroktell.contactbook.model.Client com.miroktell.contactbook.controllers.ClientController.getFullClientByPhoneNumber(java.lang.String) 2017-10-22 20:30:19 INFO RequestMappingHandlerMapping:220 - Mapped "{[/client/addNotice],methods=[POST],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public com.miroktell.contactbook.utils.MyMesage com.miroktell.contactbook.controllers.ClientController.addNotice(com.miroktell.contactbook.model.ClientNotice) 2017-10-22 20:30:19 INFO RequestMappingHandlerMapping:220 - Mapped "{[/util/filter/delete/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.miroktell.contactbook.utils.MyMesage com.miroktell.contactbook.controllers.UtilController.deleteFilter(int) 2017-10-22 20:30:19 INFO SimpleUrlHandlerMapping:315 - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler] 2017-10-22 20:30:19 INFO RequestMappingHandlerAdapter:523 - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Oct 22 20:30:18 EEST 2017]; parent: Root WebApplicationContext 

    2 answers 2

    1) You send a JSON string, respectively, you must write in the constructor the appropriate annotation for parsing data

    2) I did not work with this API branch, but it seems to me that you should play enableSimpleBroker("/events", "/topic", "/queue") with the configuration, namely enableSimpleBroker("/events", "/topic", "/queue") . You implicitly have a link by which the controller should read the data . In your case, this should be @MessageMapping("/app-dest-prefix/chat") . Try

    • Concerning the first point: Are you talking about casting json to a java object? If yes. then as far as I understand, jackson is my business. Concerning the second, where I have not explicitly indicated the link to the controller that reads the data? I tried to change the controller line to your @MessageMapping ("/ app-dest-prefix / chat"). The situation has not changed - Aleksei
    • In your controller, the received object is not marked with the corresponding '@ResponseBody annotation public Message send (@RequestBody Message Message)'. Show the mapping log of active links to controllers. Try using @MessageMapping ("/ chat") for the place using @RequestMapping (value = "/ chat", method = RequestMethod.GET, headers = {"Content-type = application / json"}) - GenCloud
    • Added an example of a mapping log. As for the annotation, '@RequestMapping is also used for http requests, and I have a web socket - Aleksei

    The problem remained unsolved. Out of the situation (until I figure it out) in this way. I send a message to the backend via the usual http, and I already send the mailing to all subscriber via the web socket.

      @RequestMapping(value = "/testChat", method = RequestMethod.POST, headers="Accept=application/json") public void testChat(@RequestBody Message message){ Logger.getLogger(NotificationController.class.getName()).log(Level.SEVERE, "RECEIVED MESSAGE FROM CLIENT: \n"+message); this.simpMessagingTemplate.convertAndSend("/topic/messages", message); Logger.getLogger(NotificationController.class.getName()).log(Level.SEVERE, "SENDED THIS MESSAGE TO ALL SUBSCRIBERS: /topic/messages"); } 

    I understand that to do so is complete nonsense, but so far the problem has not been solved, it seems like the only working way ... Thanks @GenCloud for trying to help.