-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
In a Spring Boot @RestController, a response of List<T> is serialized differently from a response of org.springframework.data.web.PagedModel<T>, which I find unexpected. The source of the difference appears to involve this line
Line 468 in 5c6622f
| javaType = getJavaType(type, null); |
A type of List<T> is classified by getJavaType as a CollectionType while a type of PagedModel<T> is classified as a SimpleType.
The immediate consequence is to affect this line
Line 476 in 5c6622f
| if (javaType != null && (javaType.isContainerType() || javaType.isTypeOrSubTypeOf(Optional.class))) { |
For objects of type List<T>, javaType.isContainerType() evaluates to true but not for PagedModel<T>. So, in the next line, List<T> receives a customised objectWriter but PagedModel<T> does not.
if (javaType != null && (javaType.isContainerType() || javaType.isTypeOrSubTypeOf(Optional.class))) {
objectWriter = objectWriter.forType(javaType);
}
One consequence of this is that if you have a REST endpoints for /widgets that returns PagedModel<T> and another for /widgets/{id} that returns a single object of type T, the entities in PagedModel<T> may be serialized differently than the single object - if you have defined polymorphic serialization for objects of type T, it will be applied for single entity responses but not for PagedModel<T> responses. A further immediate consequence is that there is no straightforward way to use polymorphic serialization with PagedModel responses.