I'm new to spring boot, and I'm trying to send back jsonstring using websocket but I don't think it is returning correct jsonstring format.
RMModel.java
public class RMModel {
private Integer inQueue;
private Integer suspended;
public RMModel getMessage() {
this.inQueue = new Random().nextInt(11);
this.suspended = new Random().nextInt(11);
return this;
}
@Override
public String toString() {
return "{" + "\"inqueue\":" + this.inQueue + "," + "\"suspended\":" + this.suspended + '}';
}
}
WebSocketScheduler.java
@Component
public class WebSocketScheduler {
@Autowired
private SimpMessagingTemplate template;
@Scheduled(fixedRate = 1000)
public void publishData() {
String data = RMModel.getData().toString();
this.template.convertAndSend("/topic/recon", data);
}
}
So I want to return RMModel's jsonstring to the client. I have angular2 client
this._stompService.subscribe('/topic/recon').subscribe(res => console.log(JSON.parse(res.body)));
It is not converting to json object. What is the correct way to return jsonstring in spring boot?
toStringto build the json object is really fishy. If you pass your RMModel to the convertAndSend, under the hood, it will convert it to a json string as you would expect it to be (it usesjacksonto accomplish that)