5ce063ebe445376ef1fb347b4181fd0cb4e01ad7
[JavaForFun] /
1 package de.spring.webservices.rest.controller;
2
3 import java.time.LocalDateTime;
4
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;
11
12
13 @RestController
14 public class RestGreetingController {
15
16         private SimpMessagingTemplate template;
17
18     @Autowired
19     public RestGreetingController(SimpMessagingTemplate template) {
20         this.template = template;
21     }
22
23     // Sending data to /topic/greeting from REST service.
24         // POST http://localhost:8080/spring-stomp-server/greetings
25     
26         @RequestMapping(path="/greetings", method=RequestMethod.POST)
27     public void handle(@RequestBody String greeting) {
28                 String text = "[" + LocalDateTime.now() + "]:" + greeting;
29                 
30                 // STOMP clients subscribed to /topic/greeting will receive the data sent by the convertAndSend method.
31                 this.template.convertAndSend("/topic/greeting", text);
32     }
33
34 }