2

My Java Code which intercepts all the calls with the URL /hello (Back) :

@Controller
public class GreetingController {

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

My JavaScript code which calls the method greeting() (Front) :

function sendName() {
    var name = document.getElementById('name').value;
    stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
}

Can some one please let me know how can I invoke @MessageMapping("/hello") method from Rest client. I can able to connect through JavaScript code and even getting response and displaying it in HTML. But I have a requirement where external system(through rest call) should invoke this @MessageMapping("/hello") and then application(backend) should send response to HTML. I tried to invoke @MessageMapping("/hello") method from chrome rest client plugin but was unsuccessful.

Please let me know how to invoke this method? Thanks in advance

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Pratap A.K
  • 4,337
  • 11
  • 42
  • 79

2 Answers2

12
@RestController
@RequestMapping("/rest")
public class GreetingController {

    private SimpMessagingTemplate template;

      @Autowired
        public GreetingController(SimpMessagingTemplate template) {
            this.template = template;
        }


    @RequestMapping(value="/hello", method=RequestMethod.POST)  
    public String greeting(@RequestBody HelloMessage message) throws Exception {

        this.template.convertAndSend("/topic/greetings", message.getName()+"asgdasd");  
        return "hello";
    }    
}

using SimpMessagingTemplate i achived this and my javascript code using sockjs which will be subscribe to /topic/greetings is

<script type="text/javascript">
        var stompClient = null;

        function setConnected(connected) {
            document.getElementById('connect').disabled = connected;
            document.getElementById('disconnect').disabled = !connected;
            document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
            document.getElementById('response').innerHTML = '';
        }

        function connect() {
            var socket = new SockJS('/hello');
            stompClient = Stomp.over(socket);            
            stompClient.connect({}, function(frame) {
                setConnected(true);
                console.log('Connected: ' + frame);
                stompClient.subscribe('/topic/greetings', function(greeting){
                    alert("inside subscribe");
                    alert(greeting);
                    showGreeting(JSON.parse(greeting.body).content);
                });
            });
        }

        function disconnect() {
            if (stompClient != null) {
                stompClient.disconnect();
            }
            setConnected(false);
            console.log("Disconnected");
        }

/*         function sendName() {
            var name = document.getElementById('name').value;
            stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
        } */

        function showGreeting(message) {
            var response = document.getElementById('response');
            var p = document.createElement('p');
            p.style.wordWrap = 'break-word';
            p.appendChild(document.createTextNode(message));
            response.appendChild(p);
        }
    </script>
Pratap A.K
  • 4,337
  • 11
  • 42
  • 79
  • Reference http://stackoverflow.com/questions/22925951/could-not-autowire-no-beans-of-simpmessagingtemplate-type-found – Pratap A.K Oct 16 '15 at 09:29
1

Since those aren't typical REST calls over HTTP, but STOMP messages sent over websocket, you should use the dedicated STOMP client provided by Spring.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176