Consistent object type exposure for JSON rendering (workaround for Gson)
Issue: SPR-16461
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -87,21 +87,16 @@ public abstract class AbstractGenericHttpMessageConverter<T> extends AbstractHtt
|
||||
|
||||
if (outputMessage instanceof StreamingHttpOutputMessage) {
|
||||
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
|
||||
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
|
||||
streamingOutputMessage.setBody(outputStream -> writeInternal(t, type, new HttpOutputMessage() {
|
||||
@Override
|
||||
public void writeTo(final OutputStream outputStream) throws IOException {
|
||||
writeInternal(t, type, new HttpOutputMessage() {
|
||||
@Override
|
||||
public OutputStream getBody() throws IOException {
|
||||
return outputStream;
|
||||
}
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
});
|
||||
public OutputStream getBody() {
|
||||
return outputStream;
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
}));
|
||||
}
|
||||
else {
|
||||
writeInternal(t, type, outputMessage);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.http.converter.json;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
@@ -34,7 +35,7 @@ import org.springframework.util.Assert;
|
||||
* By default, it supports {@code application/json} and {@code application/*+json} with
|
||||
* {@code UTF-8} character set.
|
||||
*
|
||||
* <p>Tested against Gson 2.6; compatible with Gson 2.0 and higher.
|
||||
* <p>Tested against Gson 2.8; compatible with Gson 2.0 and higher.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
* @author Juergen Hoeller
|
||||
@@ -93,7 +94,12 @@ public class GsonHttpMessageConverter extends AbstractJsonHttpMessageConverter {
|
||||
|
||||
@Override
|
||||
protected void writeInternal(Object o, @Nullable Type type, Writer writer) throws Exception {
|
||||
if (type != null) {
|
||||
// In Gson, toJson with a type argument will exclusively use that given type,
|
||||
// ignoring the actual type of the object... which might be more specific,
|
||||
// e.g. a subclass of the specified type which includes additional fields.
|
||||
// As a consequence, we're only passing in parameterized type declarations
|
||||
// which might contain extra generics that the object instance doesn't retain.
|
||||
if (type instanceof ParameterizedType) {
|
||||
getGson().toJson(o, type, writer);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.http.converter.json;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import javax.json.bind.Jsonb;
|
||||
import javax.json.bind.JsonbBuilder;
|
||||
@@ -100,7 +101,7 @@ public class JsonbHttpMessageConverter extends AbstractJsonHttpMessageConverter
|
||||
|
||||
@Override
|
||||
protected void writeInternal(Object o, @Nullable Type type, Writer writer) throws Exception {
|
||||
if (type != null) {
|
||||
if (type instanceof ParameterizedType) {
|
||||
getJsonb().toJson(o, type, writer);
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user