Manage Acccept custom header in Spring MVC

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

Since the GitHub API specification inspires me, I wanted to implement my own 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 type of mime and that it should be serialized by Json (i.e. org.springframework.web.servlet.view.json.MappingJacksonJsonView).

Any idea how to do this?

+2
source share
1 answer

You can probably do exactly what is done with org.springframework.http.converter.json.MappingJacksonHttpMessageConverter . Since it is not the final class, you can get your converter from this:

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

then register the 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.

+2
source

Source: https://habr.com/ru/post/1206263/


All Articles