如果我正在使用resteasy,我可以使用resteasy-jackson-provider来处理将我的对象编组到JSON并返回我的其他端点,例如:
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public MyThing getSingle() {
return new MyThing();
}
这很好,因为这意味着我不需要为每种类型指定编码器 - 杰克逊只是处理它。
我现在正在学习websockets,我发现我必须提供编码器:
@ServerEndpoint(value = "/websocket", encoders = {MyThingEncoder.class}, decoders = {MyThingDecoder.class})
public class Websocket {
@OnOpen
public void onOpen(Session session) {
session.getBasicRemote().sendObject(new MyThing());
}
}
这令人沮丧,因为我不想为每个单独的实体类型提供编码器/解码器,特别是如果他们只是使用Jackson。现在,我的编码器/解码器看起来像:
public class MyThingEncoder implements Encoder.Text<MyThing> {
private static final ObjectMapper MAPPER = new ObjectMapper();
@Override
public void init(EndpointConfig endpointConfig) {
}
@Override
public void destroy() {
}
@Override
public String encode(MyThing t) throws EncodeException {
try {
return MAPPER.writeValueAsString(t);
} catch (IOException e) {
throw new EncodeException(t, "Could not encode.", e);
}
}
}
我尝试通过将类定义更改为MyThingEncoder(下面)来使用泛型,但它引发了异常(也在下面):
package com.jetnuts.serv.encoders;
import org.codehaus.jackson.map.ObjectMapper;
import org.jboss.resteasy.util.Encode;
import javax.websocket.*;
import java.io.IOException;
public class MyThingEncoder<T> implements Encoder.Text<T> {
private static final ObjectMapper MAPPER = new ObjectMapper();
Class<T> typeOf;
@Override
public void init(EndpointConfig endpointConfig) {
}
@Override
public void destroy() {
}
@Override
public String encode(T t) throws EncodeException {
try {
return MAPPER.writeValueAsString(t);
} catch (IOException e) {
throw new EncodeException(t, "Could not encode.", e);
}
}
}
例外:
org.eclipse.jetty.websocket.api.InvalidWebSocketException: Invalid type declared for interface javax.websocket.Encoder$Text on class class com.jetnuts.serv.encoders.MyThingEncoder
at org.eclipse.jetty.websocket.jsr356.metadata.EncoderMetadataSet.getEncoderType(EncoderMetadataSet.java:82)
at org.eclipse.jetty.websocket.jsr356.metadata.EncoderMetadataSet.discover(EncoderMetadataSet.java:50)
at org.eclipse.jetty.websocket.jsr356.metadata.CoderMetadataSet.addAll(CoderMetadataSet.java:78)
at org.eclipse.jetty.websocket.jsr356.server.AnnotatedServerEndpointMetadata.<init>(AnnotatedServerEndpointMetadata.java:51)
at org.eclipse.jetty.websocket.jsr356.server.ServerContainer.getServerEndpointMetadata(ServerContainer.java:162)
at org.eclipse.jetty.websocket.jsr356.server.ServerContainer.addEndpoint(ServerContainer.java:87)
at org.eclipse.jetty.websocket.jsr356.server.ServerContainer.doStart(ServerContainer.java:139)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.server.handler.ScopedHandler.doStart(ScopedHandler.java:120)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:803)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:344)
at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1379)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1341)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:772)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:261)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:517)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:41)
at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:188)
at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:499)
at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:147)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:458)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610)
at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529)
at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392)
at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:329)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
如何避免需要一个编码器/解码器,它对我想要发送/接收的每种类型的对象做同样的事情?
答案 0 :(得分:3)
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import java.io.IOException;
import java.io.Reader;
public class DataDecoder implements Decoder.TextStream<Object> {
@Override
public Object decode(Reader reader) throws DecodeException, IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(reader, new TypeReference<Object>() {
});
}
@Override
public void init(EndpointConfig config) {
}
@Override
public void destroy() {
}
}
@ClientEndpoint (decoders = { DataDecoder.class })
public class SomeClass {
private WebSocketContainer container;
private Session session;
private Object objectModel;
@Override
public SomeType getAimedJobs() throws DatabaseException, ModuleNotLoadException, NamingException {
SomeType someType = new SomeType ();
ObjectMapper mapper = new ObjectMapper();
try {
String jsonInString = mapper.writeValueAsString(this.objectModel);
someType = mapper.readValue(jsonInString, SomeType.class);
} catch (IOException e) {
e.printStackTrace();
}
return someType ;
}
@OnOpen
public void onOpen(Session session) {
this.session = session;
}
@OnMessage
public void onMessage(Object o) {
this.objectModel = o;
}
@OnClose
public void onClose(CloseReason reason) {
System.out.println("WebSocket connection closed with CloseCode: " + reason.getCloseCode());
this.session.Close();
}
}