1

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?

4
  • do you get any error or something? In any case, having that toString to 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 uses jackson to accomplish that) Commented Jul 18, 2018 at 16:33
  • That's what I thought too. But the client doesn't receive anything but string object... I have no idea why Commented Jul 18, 2018 at 16:34
  • try sending the RMModel directly and not send it like a string and see if it is still the problem. I only have a working angular 1.x that I tested on and it worked fine. Maybe angular 2 is a different kind of beast xD Commented Jul 18, 2018 at 16:36
  • I have tried that, there's no error on the server side, but the client side doesn't receive anything... Commented Jul 18, 2018 at 16:37

1 Answer 1

1

problem solved. The model shouldn't have a method that returns itself, jackson will throw an exception.

RMModel.java

public class RMModel {
    private Integer inQueue;
    private Integer suspended;

    public Integer getInQueue() {
        return inQueue;
    }

    public void setInQueue(Integer maximum) {
        this.inQueue = new Random().nextInt(maximum);
    }

    public Integer getSuspended() {
        return suspended;
    }

    public void setSuspended(Integer maximum) {
        this.suspended = new Random().nextInt(maximum);
    }

    @Override
    public String toString() {
        return "{" + "\"inqueue\":" + this.inQueue + "," + "\"suspended\":" + this.suspended + '}';
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I guess that content-type will be text/plain instead of application/json. In my case if I try to manually set the content type of the message then the JSON is escaped (so it is actually a JSON string not a JSON object).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.