Consistent object type exposure for JSON rendering (workaround for Gson)

Issue: SPR-16461
This commit is contained in:
Juergen Hoeller
2018-02-05 22:51:33 +01:00
parent 31b25b5808
commit 817a836960
6 changed files with 363 additions and 127 deletions

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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 {