【问题标题】:How do I make Jersey use Java serialization?如何让 Jersey 使用 Java 序列化?
【发布时间】:2013-02-25 19:03:10
【问题描述】:

我意识到它有点非 RESTful,但我想使用 Jersey 客户端将 Spring Integration GenericMessage 发送到使用 Java 序列化作为“编组”的 Jersey 服务器。是否有内置或贡献支持,或者我必须编写自己的MessageBodyReader/Writer?我已经尝试过setting the type in the Client request"application/x-java-serialized-object"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

Google 和Jersey User Guide 都奇怪地对这个话题保持沉默。我也接受另一种方法来允许将任意 GenericMessage 发送到 Jersey API。我不一定要进行序列化。这似乎是处理具有任意有效负载类型的消息的最简单途径。

【问题讨论】:

    标签: java jersey spring-integration


    【解决方案1】:

    我还没有找到一个,而且显然没有其他人知道,所以我使用Commons Lang's 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      相关资源
      最近更新 更多