diff --git a/java-websocket/src/main/java/com/baeldung/websocket/MessageDecoder.java b/java-websocket/src/main/java/com/baeldung/websocket/MessageDecoder.java index 29ae5b93e6..3bb3c4391d 100644 --- a/java-websocket/src/main/java/com/baeldung/websocket/MessageDecoder.java +++ b/java-websocket/src/main/java/com/baeldung/websocket/MessageDecoder.java @@ -8,9 +8,11 @@ import com.baeldung.model.Message; import com.google.gson.Gson; public class MessageDecoder implements Decoder.Text { + + private static Gson gson = new Gson(); + @Override public Message decode(String s) throws DecodeException { - Gson gson = new Gson(); Message message = gson.fromJson(s, Message.class); return message; } diff --git a/java-websocket/src/main/java/com/baeldung/websocket/MessageEncoder.java b/java-websocket/src/main/java/com/baeldung/websocket/MessageEncoder.java index bfecc87a96..6e7ba06ff0 100644 --- a/java-websocket/src/main/java/com/baeldung/websocket/MessageEncoder.java +++ b/java-websocket/src/main/java/com/baeldung/websocket/MessageEncoder.java @@ -8,9 +8,11 @@ import com.baeldung.model.Message; import com.google.gson.Gson; public class MessageEncoder implements Encoder.Text { + + private static Gson gson = new Gson(); + @Override public String encode(Message message) throws EncodeException { - Gson gson = new Gson(); String json = gson.toJson(message); return json; }