* Project for " A Guide to the Java API for WebSocket" article * Setting dependencies correctly * Formatting adjustments * Removing tomcat7 maven plugin * Applying formatt - No spaces * BAEL-389 - Building URL dynamically between host and pathname * Setting javax websocket api scope to provided * Make Gson static field
30 lines
662 B
Java
30 lines
662 B
Java
package com.baeldung.websocket;
|
|
|
|
import javax.websocket.EncodeException;
|
|
import javax.websocket.Encoder;
|
|
import javax.websocket.EndpointConfig;
|
|
|
|
import com.baeldung.model.Message;
|
|
import com.google.gson.Gson;
|
|
|
|
public class MessageEncoder implements Encoder.Text<Message> {
|
|
|
|
private static Gson gson = new Gson();
|
|
|
|
@Override
|
|
public String encode(Message message) throws EncodeException {
|
|
String json = gson.toJson(message);
|
|
return json;
|
|
}
|
|
|
|
@Override
|
|
public void init(EndpointConfig endpointConfig) {
|
|
// Custom initialization logic
|
|
}
|
|
|
|
@Override
|
|
public void destroy() {
|
|
// Close resources
|
|
}
|
|
}
|