How to configure custom MediaType in Spring MVC?

一个人想着一个人 提交于 2019-11-28 03:54:00

问题


Using Spring MVC, I have controllers already working for both JSON and XML media formats. In the content negotiation configuration, I would like to rely on Accept header only, and introduce a custom name media type, for example: "myXml"

My configuration:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer
           .favorPathExtension(false)
           .favorParameter(false)
           .ignoreAcceptHeader(false)
           .useJaf(false)
           .mediaType(MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON)
           .mediaType(MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_XML)
           .mediaType("myXml", MediaType.APPLICATION_XML)
           .defaultContentType(MediaType.APPLICATION_JSON);
    }
}

My Controller:

@RequestMapping(value = "/manager/{id}",
        method = RequestMethod.GET,
        produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}
)
@ResponseBody public Manager managers(@PathVariable long id){
    return repo.getManagerById(id);
}

It works pretty well, Accept header: application/json produces JSON, application/xml produces XML. Anything else returns 406 Not Acceptable, even myXml.

I expected xml though...


回答1:


With that configuration, you basically:

  • ignored content negotiation using params or path extensions
  • registered "json -> application/json" "xml -> application/xml" "myXml -> application/xml" as possible path extensions / params for negotiating to those media types. (see more about this here)
  • told Spring MVC that whenever the HTTP client sends "Accept: */*" or no Accept header at all, the default ContentType should be "application/xml"

I don't think you intended to handle content negotiation like this.

You probably want to customize HttpMessageConverters (see here), like registering a Jaxb2RootElementHttpMessageConverter (if using JAXB) or a MappingJackson2XmlHttpMessageConverter (if using Jackson) and registering them with both "application/xml" and "myXml" media types.

Also, don't forget to add "myXml" in the "produces" part of the RequestMapping annotation - your controller method should declare it as a media type it can produce, otherwise it will will throw 406 again.

My advice

You should definitely use a media type like "application/vnd.foobar.v.1.0+xml" since:

  • this is something relevant to http clients
  • xml HttpMessageConverters in Spring are already registered to deal with "application/xml" and "application/*+xml".

In that case you can juste keep the defaultContentType part in your configuration (and probably set it to your custom media type) and throw away the rest.

In any case, you should still declare this custom media type in the produces section of your mapping annotations.



来源:https://stackoverflow.com/questions/27185375/how-to-configure-custom-mediatype-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!