I need to rewrite the GsonJsonProvider using the Jackson library.
This is the original code:
@Component(immediate = true)
@Provider
public class GsonJsonProvider implements MessageBodyReader<Object>, MessageBodyWriter<Object>, JaxRsComponent {
private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return isApplicationJson(mediaType);
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return isApplicationJson(mediaType);
}
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) {
// To conform with readFrom specification, reader and entityStream is not closed
InputStreamReader reader = new InputStreamReader(entityStream, MessageUtils.getCharset(mediaType));
if (genericType == null) {
return gson.fromJson(reader, type);
} else {
return gson.fromJson(reader, genericType);
}
}
@Override
public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
// To conform with writeTo specification, writer and entityStream is not closed
OutputStreamWriter writer = new OutputStreamWriter(entityStream, MessageUtils.getCharset(mediaType));
if (genericType == null) {
gson.toJson(o, writer);
} else {
gson.toJson(o, genericType, writer);
}
writer.flush();
}
private boolean isApplicationJson(MediaType mediaType) {
return mediaType.equals(MediaType.APPLICATION_JSON_TYPE);
}
}
And this is my rewriten code that is not working:
@Component(immediate = true)
@Provider
public class JacksonJsonProvider implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
private ObjectMapper objectMapper;
public JacksonJsonProvider() {
objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return isApplicationJson(mediaType);
}
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
InputStreamReader reader = new InputStreamReader(entityStream, MessageUtils.getCharset(mediaType));
if (genericType == null) {
return objectMapper.readValue(reader, type);
} else {
return objectMapper.readValue(reader, objectMapper.getTypeFactory().constructType(genericType));
}
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return isApplicationJson(mediaType);
}
@Override
public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(entityStream, MessageUtils.getCharset(mediaType));
if (genericType == null) {
objectMapper.writeValue(writer, o);
} else {
objectMapper.writerFor(objectMapper.getTypeFactory().constructType(genericType)).writeValue(writer, o);
}
writer.flush();
}
private boolean isApplicationJson(MediaType mediaType) {
return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
}
When I do a POST request to upload data for an asset I get the following:
Views
Replies
Total Likes
Hello @anasustic -
The issue seems to be related to a closed input stream.The InputStream is being closed somewhere before it's being read by the ObjectMapper.
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
try (InputStreamReader reader = new InputStreamReader(entityStream, MessageUtils.getCharset(mediaType))) {
if (genericType == null) {
return objectMapper.readValue(reader, type);
} else {
return objectMapper.readValue(reader, objectMapper.getTypeFactory().constructType(genericType));
}
}
}
Hi @Tanika02
Thanks for your reply
Can you suggest a remedy for my issue?
The original GsonJsonProvider.java is working flawlessly. The new JacksonJsonProvider that I rewrote is giving me errors. Everything else is the same.
Views
Likes
Replies
Views
Likes
Replies