I am updating an application from Spring Platform version 1.1.3.RELEASE to 2.0.1.RELEASE, which bumps the Spring Framework version from 4.1.7 to 4.2.4, and Jackson from 2.4.6 to
Your configuration is ok. The reason why writeInternal()
is not called from your custom converter is because you are overriding the wrong method.
Looking at the source code of 4.2.4.RELEASE
AbstractMessageConverterMethodProcessor#writeWithMessageConverters
protected void writeWithMessageConverters(T returnValue, MethodParameter returnType,
ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
...
((GenericHttpMessageConverter) messageConverter).write(returnValue, returnValueType, selectedMediaType, outputMessage);
...
}
AbstractGenericHttpMessageConverter#write
public final void write(final T t, final Type type, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
...
writeInternal(t, type, outputMessage);
...
}
The method writeInternal(...)
called from within AbstractGenericHttpMessageConverter#write(...)
has three arguments - (T t, Type type, HttpOutputMessage outputMessage)
. You are overriding the overloaded version of writeInternal(...)
that has only 2 arguments - (T t, HttpOutputMessage outputMessage)
.
However, in version 4.1.7.RELEASE
, it is not the case, hence the root cause of your problem. The writeInternal(...)
used in this version is the other overloaded method (the method with 2 arguments) that you have overriden. This explains why it is working fine in 4.1.7.RELEASE
.
@Override
public final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
...
writeInternal(t, outputMessage);
...
}
So, to solve your problem, instead of overriding writeInternal(Object object, HttpOutputMessage outputMessage)
, override writeInternal(Object object, Type type, HttpOutputMessage outputMessage)