如何在Spring MVC中配置自定义MediaType? [英] How to configure custom MediaType in Spring MVC?

查看:986
本文介绍了如何在Spring MVC中配置自定义MediaType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring MVC,我已经有适用于JSON和XML媒体格式的控制器. 在内容协商配置中,我只想依靠Accept标头,并引入一个自定义名称媒体类型,例如:"myXml"

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"

我的配置:

@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);
    }
}

我的控制器:

@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);
}

它工作得很好,Accept标头:application/json产生JSON,application/xml产生XML.其他任何内容都返回406不可接受,甚至是myXml.

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

虽然我希望xml ...

I expected xml though...

推荐答案

使用该配置,您基本上可以:

With that configuration, you basically:

  • 使用参数或路径扩展忽略内容协商
  • 将"json-> application/json""xml-> application/xml""myXml-> application/xml"注册为可能的路径扩展名/参数,以协商这些媒体类型. (在此处查看有关此内容的更多信息)
  • 告诉Spring MVC,无论HTTP客户端何时发送"Accept: */*"或根本不发送Accept标头,默认的ContentType应为"application/xml"
  • 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.

您可能想要自定义HttpMessageConverters(

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.

此外,不要忘记在RequestMapping批注的产生"部分中添加"myXml"-您的控制器方法应将其声明为可以产生的媒体类型,否则它将再次抛出406.

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.

您绝对应该使用诸如"application/vnd.foobar.v.1.0 + xml"之类的媒体类型,因为:

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

  • 这与http客户有关
  • 在Spring中,xml HttpMessageConverters已被注册以处理"application/xml"和"application/* + xml".

在这种情况下,您可以将defaultContentType部分保留在配置中(并可能将其设置为自定义媒体类型),然后丢弃其余部分.

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.

无论如何,您仍然应该在映射注释的Produces部分中声明此自定义媒体类型.

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

这篇关于如何在Spring MVC中配置自定义MediaType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆