如何使Jersey使用Java序列化? [英] How do I make Jersey use Java serialization?
问题描述
我意识到这有点不稳定,但是我想使用Jersey客户端发送Spring Integration 作家?我已经尝试过" application/x-java-serialized-object"和
I realize it's a little un-RESTful, but I'd like to use a Jersey client to send a Spring Integration GenericMessage
to a Jersey server using Java serialization as the "marshalling". Is there built-in or contrib support for that, or will I have to write my own MessageBodyReader/Writer? I've tried setting the type in the Client request to both "application/x-java-serialized-object" and MediaType.APPLICATION_OCTET_STREAM_TYPE
, and it just gives the usual:
ClientHandlerException: A message body writer for Java type,
class org.springframework.integration.message.GenericMessage,
and MIME media type, application/x-java-serialized-object, was not found
Google和《泽西岛用户指南》 都奇怪地沉默了关于这个问题.我也接受另一种方法,允许将任意GenericMessage
发送到Jersey API.我不一定要进行序列化.似乎是处理具有任意有效负载类型的消息的最简单方法.
Both Google and the Jersey User Guide are strangely silent on this subject. I'd also accept an alternate way to allow sending an arbitrary GenericMessage
to a Jersey API. I'm not necessarily set on serialization. It just seems the easiest route for handling messages with arbitrary payload types.
推荐答案
I still haven't found one, and apparently nobody else knows of one either, so I wrote my own quick implementation using Commons Lang's SerializationUtils to do the dirty work for me:
import org.apache.commons.lang.SerializationUtils;
import org.springframework.stereotype.Component;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
/**
* Created with IntelliJ IDEA.
* User: ryan
* Date: 2/25/13
* Time: 2:07 PM
*/
@Component
@Provider
public class SerializationMessageBodyReaderAndWriter
implements MessageBodyReader<Serializable>, MessageBodyWriter<Serializable> {
public static final String APPLICATION_JAVA_SERIALIZED_OBJECT =
"application/x-java-serialized-object";
public static final MediaType APPLICATION_JAVA_SERIALIZED_OBJECT_TYPE =
MediaType.valueOf(APPLICATION_JAVA_SERIALIZED_OBJECT);
@Override
public boolean isReadable(Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType) {
return mediaType.isCompatible(APPLICATION_JAVA_SERIALIZED_OBJECT_TYPE)
&& Serializable.class.isAssignableFrom(type);
}
@Override
public Serializable readFrom(Class<Serializable> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) {
return (Serializable) SerializationUtils.deserialize(entityStream);
}
@Override
public boolean isWriteable(Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType) {
return mediaType.isCompatible(APPLICATION_JAVA_SERIALIZED_OBJECT_TYPE)
&& Serializable.class.isAssignableFrom(type);
}
@Override
public long getSize(Serializable o,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType) {
return -1;
}
@Override
public void writeTo(Serializable o,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) {
SerializationUtils.serialize(o, entityStream);
}
}
这篇关于如何使Jersey使用Java序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!