Affects: 2.7.7
Problem
MappingJackson2HttpMessageConverter
ignores the declared (possibly generic) target type, and consults only the dynamic class of the value being written. Without the declared type information, the Jackson module I'm using is unable to locate the right serializer, and throws IllegalStateException
.
Root cause
MappingJackson2HttpMessageConverter
inherits its implementation of the 3-argument method canWrite
from AbstractGenericHttpMessageConverter
, which is implemented as follows:
@Override
public boolean canWrite(@Nullable Type type, Class<?> clazz, @Nullable MediaType mediaType) {
return canWrite(clazz, mediaType);
}
Note that this ignores the type
parameter (which specifies the declared, possibly generic, type) and uses only the clazz
parameter (which specifies the dynamic, erased class of the value being written).
The Jackson module I'm using relies on the generic type information to do correct serialization.
Potential fix
- Move the implementation logic in
AbstractJackson2HttpMessageConverter.canWrite
from the 2-argument version to the 3-argument version, and change it to consider the static type information. - Change the 2-argument
canWrite
to call the 3-argument version with anull
type.
Comment From: prdoyle
Hello! It has been six months since I opened this. Is there anything I can do to help make additional progress?
I can submit a pull request if that would be helpful. If my analysis is right, this is a 2-line fix.
Comment From: prdoyle
FYI we've found a way to change the Jackson module so it doesn't rely on declared type information. This issue is no longer blocking us.
Comment From: snicoll
@prdoyle unfortunately, partial code snippet in text as in here aren't a great way to get support. Can you please share a small sample we can run that demonstrates the use of the API and why it lead to the problem you've described?
You can do that by attaching a zip to this issue or pushing the code to a GitHub repository.
Comment From: prdoyle
Hey @snicoll. With the resolution I found, I now kind of consider this to be a problem on my end, trying to use Jackson's type information in a way that runs against the grain. I'm happy to consider this matter closed.
Comment From: snicoll
I see, I was suspecting as much and wanted to confirm with the sample rather than guessing. Thanks a lot for the feedback!