2

I have a RESTful web service developed using Spring MVC and without any configuration I can return objects from my @ResponseBody annotated controller methods that get serialized to JSON. This works as soon as the Accept header in the request is not set or is application/json.

As I'm getting inspired by the GitHub API specification, I wanted to implement custom mime type for my API as GitHub does, for example: application/vnd.myservice+json. But then I need to tell Spring MVC that my controllers can provide this mime type and that it should be serialized by Json (i.e org.springframework.web.servlet.view.json.MappingJacksonJsonView class).

Any idea how to do it?

MacJariel
  • 55
  • 6

1 Answers1

2

You can probably do exactly what is being done with org.springframework.http.converter.json.MappingJacksonHttpMessageConverter. Since it is not a final class, you can derive your converter from this one this way:

class MyCustomVndConverter extends MappingJacksonHttpMessageConverter{
    public MyCustomVndConverter (){
        super(MediaType.valueOf("application/vnd.myservice+json"));
    }
}

then register your converter this way:

<mvc:annotation-driven> 
   <mvc:message-converters register-defaults="true">
       <bean class="MyCustomVndConverter "/>
   </mvc:message-converters>
</mvc:annotation-driven>

It should just work with these changes

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Thanks, you pointed me in the right direction. I only needed to call `setSupportedMediaTypes(Collections.singletonList(MediaType.valueOf("application/vnd.myservice+json")));` instead of calling `super` constructor. BTW. it's a shame that MappingJacksonHttpMessageConverter doesn't come with something like `super(MediaType.valueOf("application/*+json"))` in its contructor, because it would work out of the box then. – MacJariel Aug 20 '12 at 02:38