【问题标题】:Vendor MIME types and API versioning in Symfony2 with FOSRestBundle and JMSSerializerBundleSymfony2 中的供应商 MIME 类型和 API 版本控制与 FOSRestBundle 和 JMSSerializerBundle
【发布时间】:2013-11-12 21:56:23
【问题描述】:

我想处理带有请求标头“接受”的资源的不同版本

===>
GET /customer/123 HTTP/1.1
Accept: application/vnd.company.myapp.customer-v3+json

<===
HTTP/1.1 200 OK
Content-Type: application/vnd.company.myapp-v3+json

{"customer":
  {"name":"Neil Armstrong"}
}

我想读取 Accept 标头,提取版本并使用此版本初始化序列化程序!

FOSRestBundle 可以做到这一点吗?

我知道我可以监听 kernel.request,检查“Accept”标头并从 MIME 类型中提取请求的版本,但我不知道如何使用此值设置序列化程序...

我还阅读了 FOSRestBundle 文档,我知道您可以在配置中添加新的 mime 类型:

view.mime_types: {'json': ['application/vnd.company.myapp.customer-v1+xml', 'application/vnd.company.myapp.customer-v2+xml']}

我需要在此处添加所有可能的 mime 类型(不同版本)?

任何帮助将不胜感激!

【问题讨论】:

    标签: symfony fosrestbundle jmsserializerbundle


    【解决方案1】:

    这暂时对您没有帮助,但存在有关 API 版本控制的未解决问题。 https://github.com/FriendsOfSymfony/FOSRestBundle/pull/632

    【讨论】:

      【解决方案2】:

      现在可以实现了,请参阅the documentation

      您的 config.yml 文件看起来像这样(使用您喜欢的任何正则表达式)。

      fos_rest:
          versioning:
              enabled: true
              resolvers:
                  media_type: # Accept header
                      enabled: true
                      regex: '/v(?P<version>[0-9\.]+)\+(json|xml)/'
              guessing_order:
                  - media_type # Not needed if there's only one
          format_listener: # Must enable format_listener for versioning in Accept headers
              enabled: true
              rules: # Catch all routes to use this rule
                  - { path: '*', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: false }
          view:
              mime_types: # If there are lots of versions, can make this more dynamic. Check docs.
                  json: ['application/vnd.company.myapp.customer-v1+xml', 'application/vnd.company.myapp.customer-v2+xml', 'application/vnd.company.myapp.customer-v3+json']
      

      【讨论】:

        猜你喜欢
        • 2013-09-28
        • 2023-03-25
        • 2012-10-13
        • 2011-06-12
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 2013-12-24
        • 1970-01-01
        相关资源
        最近更新 更多