1 package de.spring.webservices.rest.controller;
3 import java.time.LocalDateTime;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.messaging.simp.SimpMessagingTemplate;
7 import org.springframework.web.bind.annotation.RequestBody;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springframework.web.bind.annotation.RestController;
14 public class RestGreetingController {
16 private SimpMessagingTemplate template;
19 public RestGreetingController(SimpMessagingTemplate template) {
20 this.template = template;
23 // Sending data to /topic/greeting from REST service.
24 // POST http://localhost:8080/spring-stomp-server/greetings
26 @RequestMapping(path="/greetings", method=RequestMethod.POST)
27 public void handle(@RequestBody String greeting) {
28 String text = "[" + LocalDateTime.now() + "]:" + greeting;
30 // STOMP clients subscribed to /topic/greeting will receive the data sent by the convertAndSend method.
31 this.template.convertAndSend("/topic/greeting", text);