如何从rest客户端调用Spring STOMP Web套接字方法

时间:2015-09-28 15:09:55

标签: spring stomp rest-client spring-websocket sockjs

我的Java代码使用URL / hello拦截所有调用(返回):

@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() + "!");       
    }    
}

我的JavaScript代码调用方法greeting()(Front):

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

有人可以告诉我如何从Rest客户端调用@MessageMapping(" / hello")方法。我可以通过JavaScript代码连接,甚至可以获得响应并以HTML格式显示它。 但是我有一个要求,外部系统(通过休息调用)应该调用这个@MessageMapping(" / hello")然后应用程序(后端)应该发送对HTML的响应。 我试图从chrome rest客户端插件调用@MessageMapping(" / hello")方法,但是没有成功。

请告诉我如何调用此方法? 提前致谢

2 个答案:

答案 0 :(得分:6)

@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";
    }    
}

使用SimpMessagingTemplate我得到了这个和我的javascript代码使用sockjs将订阅/ topic / greetings是

<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>

答案 1 :(得分:1)

由于这些不是通过HTTP进行的典型REST调用,而是通过websocket发送的STOMP消息,因此您应该使用the dedicated STOMP client provided by Spring