I'm trying to create a converter for a custom media-type like application/vnd.custom.hal+json
. I saw this answer here, but it won't work since you don't have access to the protected constructor of AbstractHttpMessageConverter<T>
(super class of MappingJackson2HttpMessageConverter
). Which means that the following code does not work:
class MyCustomVndConverter extends MappingJacksonHttpMessageConverter {
public MyCustomVndConverter (){
super(MediaType.valueOf("application/vnd.myservice+json"));
}
}
However, the following does work and basically just mimics what the constructor actually does anyway:
setSupportedMediaTypes(Collections.singletonList(
MediaType.valueOf("application??/vnd.myservice+json")
));
So I did this for my class, and then added the converter to my existing list of converters by following Spring Boot's documentation here. My code basically looks like this:
//Defining the converter; the media-type is simply a custom media-type that is
//still application/hal+json, i.e., JSON with some additional semantics on top
//of what HAL already adds to JSON
public class TracksMediaTypeConverter extends MappingJackson2HttpMessageConverter {
public TracksMediaTypeConverter() {
setSupportedMediaTypes(Collections.singletonList(
new MediaType("application??", "vnd.tracks.v1.hal+json")
));
}
}
//Adding the message converter
@Configuration
@EnableSwagger
public class MyApplicationConfiguration {
...
@Bean
public HttpMessageConverters customConverters() {
return new HttpMessageConverters(new TracksMediaTypeConverter());
}
}
As per the documentation, this should work. But what I noticed is that this has the effect of replacing the existing MappingJackson2HttpMessageCoverter
, which handles application/json;charset=UTF-8
and application/*+json;charset=UTF-8
.
I verified this by attaching a debugger to my app and stepping through breakpoints inside Spring's AbstractMessageCoverterMethodProcessor.java
class. There, the private field messageConverters
contains the list of converters that have been registered. Normally, i.e., if I do not try to add my converter, I see the following coverters:
MappingJackson2HttpMessageCoverter
for application/hal+json
(I'm assuming this is added by Spring HATEOAS, which I am using)
ByteArrayHttpMessageConverter
StringHttpMessageConverter
ResourceHttpMessageConverter
SourceHttpMessageConverter
AllEncompassingFormHttpMessageConverter
MappingJackson2HttpMessageConverter
for application/json;charset=UTF-8
and application/*+json;charset=UTF-8
Jaxb2RootElementHttpMessageConverter
When I add my custom media type, the second instance of MappingJackson2HttpMessageConverter
gets replaced. That is, the list now looks like this:
MappingJackson2HttpMessageConverter
for application/hal+json
(I'm assuming this is added by Spring HATEOAS, which I am using)
ByteArrayHttpMessageConverter
StringHttpMessageConverter
ResourceHttpMessageConverter
SourceHttpMessageConverter
AllEncompassingFormHttpMessageConverter
MappingJackson2HttpMessageConverter
for application/vnd.tracks.v1.hal+json
(the existing one has been replaced)
Jaxb2RootElementHttpMessageConverter
I'm not entirely sure why this is happening. I stepped through the code and the only thing that really happens is that the no-args constructor of MappingJackson2HttpMessageConverter
is called (as it should be), which initially sets the supported media-types to application/json;charset=UTF-8
and application/*+json;charset=UTF-8
. After that, the list gets overwritten with the media-type that I provide.
What I cannot understand is why adding this media type should replace the existing instance of MappingJackson2HttpMessageConverter
that handles regular JSON. Is there some strange magic that is going on that does this?
Currently I have a workaround, but I don't like it very much since it's not that elegant and it involves duplication of code already in MappingJackson2HttpMessageConverter
.
I created the following class (only changes from the regular MappingJackson2HttpMessageConverter
are shown):
public abstract class ExtensibleMappingJackson2HttpMessageConverter<T> extends AbstractHttpMessageConverter<T> implements GenericHttpMessageConverter<T> {
//These constructors are not available in `MappingJackson2HttpMessageConverter`, so
//I provided them here just for convenience.
/**
* Construct an {@code AbstractHttpMessageConverter} with no supported media types.
* @see #setSupportedMediaTypes
*/
protected ExtensibleMappingJackson2HttpMessageConverter() {
}
/**
* Construct an {@code ExtensibleMappingJackson2HttpMessageConverter} with one supported media type.
* @param supportedMediaType the supported media type
*/
protected ExtensibleMappingJackson2HttpMessageConverter(MediaType supportedMediaType) {
setSupportedMediaTypes(Collections.singletonList(supportedMediaType));
}
/**
* Construct an {@code ExtensibleMappingJackson2HttpMessageConverter} with multiple supported media type.
* @param supportedMediaTypes the supported media types
*/
protected ExtensibleMappingJackson2HttpMessageConverter(MediaType... supportedMediaTypes) {
setSupportedMediaTypes(Arrays.asList(supportedMediaTypes));
}
...
//These return Object in MappingJackson2HttpMessageConverter because it extends
//AbstractHttpMessageConverter<Object>. Now these simply return an instance of
//the generic type.
@Override
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
JavaType javaType = getJavaType(clazz, null);
return readJavaType(javaType, inputMessage);
}
@Override
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
JavaType javaType = getJavaType(type, contextClass);
return readJavaType(javaType, inputMessage);
}
private T readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
try {
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
}
catch (IOException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
}
}
...
}
I then use this class as follows:
public class TracksMediaTypeConverter extends ExtensibleMappingJackson2HttpMessageConverter<Tracks> {
public TracksMediaTypeConverter() {
super(new MediaType("application", "application/vnd.tracks.v1.hal+json"));
}
}
The registration of the converter in the configuration class is the same as before. With these changes, the existing instance of MappingJackson2HttpMessageConverter
is not overwritten and everything works as I would expect.
So to boil everything down, I have two questions:
- Why is the existing converter being overwritten when I extend
MappingJackson2HttpMessageConverter
?
- What is the right way to create a custom media-type converter that represents a semantic media-type that is still basically JSON (and therefore can be serialized and deserialized by
MappingJackson2HttpMessageConverter
?
See Question&Answers more detail:
os