나는 비슷한 문제가 있으며, 2 년 내에 다른 누구도 해결책을 찾지 못한 것을 알고 놀랐습니다. 우리는 Jersey 2.x를 사용하고 Avro를 처리하기 위해 Provider
을 사용했습니다.
코드를 생성하면이 스 니펫이 작동합니다. 그렇지 않은 경우 SpecificDataReader/Writer
및 SpecificRecord
대신 GenericDatumReader/Writer
및 GenericRecord
을 사용해야합니다.
또한 Avro 사양은 콘텐츠 유형으로 avro/binary
을 사용한다고 밝혔지만 잘못된 유형이므로 변경하려면 6 세의 JIRA ticket이 있습니다.
나는 이것을 단순화하기 위해 이것을 제거 했으므로 오류 처리가 없습니다. avro 바이너리를 생성하는 방법을 모르기 때문에 일반적인 예외를 포착하는 공통 인 ExceptionMapper
을 가지고 있다면주의하십시오.
@Provider
@Consumes("avro/binary")
@Produces("avro/binary")
public class AvroProvider <T extends SpecificRecord> implements MessageBodyWriter<T>, MessageBodyReader<T>
{
public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType)
{
return SpecificRecord.class.isAssignableFrom(type);
}
public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType)
{
return true;
}
@Override
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException
{
DatumReader<T> reader = new SpecificDatumReader<>(type);
Decoder decoder = DecoderFactory.get().binaryDecoder(entityStream, null);
return reader.read(null, decoder);
}
@SuppressWarnings("unchecked")
@Override
public void writeTo(T message, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException
{
DatumWriter<T> datumWriter = new SpecificDatumWriter<>((Class<T>)type);
Encoder encoder = EncoderFactory.get().binaryEncoder(entityStream, null);
datumWriter.write(message, encoder);
encoder.flush();
}
@Override
public long getSize(T message, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
return -1;
}
}
출처
2016-09-13 15:55:06
max