1

I am passing a json object to the client side from java object with a time and value as attributes with gson

this.template.convertAndSend("/topic/123", gson.toJson(object, type));

and on the client side i have the following code where the json object data is stored in the body of the payload but I am unable to access the properties with obj.time or obj.value, it tells me undefined after it is parsed, I tried showing the entire 'obj' itself and the format seems fine however:

var subscription_callback1 = function(payload) {
        var obj = JSON.parse(payload.body);
        alert(obj);
};

output with alert(obj)

{"time":"3:00:34","value":"7989797"}

1 Answer 1

1

Nevermind solved. Since I am transfering STOMP protocol messages with the Spring 4 framework. I opted to use the Jackson2 message converter instead of directly using gson and it seems to work

@Configuration
@EnableWebSocketMessageBroker
public class MessageBrokerConfigurer extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
    messageConverters.add(new MappingJackson2MessageConverter());
    return true;
}

then i directly put my java object into the send function instead of using gson to convert it as above

this.template.convertAndSend("/topic/123", event)
Sign up to request clarification or add additional context in comments.

Comments

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.