我有一个附件属性为LOB的实体。在通过Jackson JSON转换获取REST服务响应时,出现以下异常。 我在Spring JPA中使用Spring Boot
@Lob
@Column(name = "attachment")
private byte[] attachment;
at com.fasterxml.jackson.module.afterburner.ser.ObjectMethodPropertyWriter.serializeAsField(ObjectMethodPropertyWriter.java:87) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) at com.fasterxml.jackson.module.afterburner.ser.ObjectMethodPropertyWriter.serializeAsField(ObjectMethodPropertyWriter.java:87) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) at com.fasterxml.jackson.module.afterburner.ser.ObjectMethodPropertyWriter.serializeAsField(ObjectMethodPropertyWriter.java:87) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689) 2018-08-05 20:24:19.862 WARN 2084 --- [ XNIO-2 task-12] .m.m.a.ExceptionHandlerExceptionResolver : Failed to invoke @ExceptionHandler method: public default org.springframework.http.ResponseEntity org.zalando.problem.spring.web.advice.general.ThrowableAdviceTrait.handleThrowable(java.lang.Throwable,org.springframework.web.context.request.NativeWebRequest) java.io.IOException: UT010029: Stream is closed at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:136) at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:639) at com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2039) at com.fasterxml.jackson.core.json.UTF8JsonGenerator.flush(UTF8JsonGenerator.java:1051) at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:953) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:286) at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:106) at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231) at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203) at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
答案 0 :(得分:0)
您需要使用字节数组序列化:
字节数组序列化器:
public class ByteArraySerializer extends JsonSerializer<byte[]> {
@Override
public void serialize(byte[] bytes, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeStartArray();
for (byte b : bytes) {
jgen.writeNumber(unsignedToBytes(b));
}
jgen.writeEndArray();
}
private static int unsignedToBytes(byte b) {
return b & 0xFF;
}
}
使用序列化器:
@Lob
@Column(name = "attachment")
@JsonSerialize(using= ByteArraySerializer.class)
private byte[] attachment;