我如何利用泽西Java序列化?(How do I make Jersey use Java seri

2019-08-17 03:42发布

我知道这是一个有点非RESTful的,但我想用一个Jersey客户端发送一个Spring集成GenericMessage使用Java序列化的“编组”泽西服务器。 是否有内置或为支持的contrib,或者我会写我自己化MessageBodyReader / 作家 ? 我试图在客户端请求设置类型既“应用程序/ x-java的序列化对象”和MediaType.APPLICATION_OCTET_STREAM_TYPE ,它只是给出了常用的:

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

谷歌和在新泽西用户指南是对这个问题格外安静。 我也是会接受的另一种方法允许发送任意GenericMessage到新泽西API。 我不一定序列化设置。 这似乎只是为任意载荷类型处理消息的最简单的途径。

Answer 1:

我还没有找到一个,以及一个显然没人知道要么,所以我写了用自己的快速实现共享郎的SerializationUtils做肮脏的工作对我来说:

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);
    }
}


文章来源: How do I make Jersey use Java serialization?