Rails API 版本控制、路由问题
Rails API Versioning, Routing issue
我正在尝试基于 Railscasts #350 episode 实现一个简单的 rails api 带版本控制的应用程序。我想通过 Accept header
以这种格式提供版本来访问应用程序的特定 API 版本:application/vnd.rails5_api_test.v1
。当没有提供 Accept header
时,请求将被路由到应用程序的当前默认版本。为了处理这个问题,我在我的 lib directory
中创建了一个 api_constraints
文件,这是路由中所必需的。
我创建了两个版本的应用 v1
和 v2
,其中 v1
有 Users resource
和 v2
有 Users and Comments resources
.
一切都按预期工作,除了当我使用 Postman 通过 headers 传递版本 1
来请求 URL localhost:3000/comments
时,我从 comments resource
, 显示所有评论。但我希望响应是 status: 404 Not Found
,因为 comments resource
的版本是 2
,而请求的版本是 1
。
这是服务器的响应:
Started GET "/comments" for 127.0.0.1 at 2016-04-01 20:57:53 +0530
Processing by Api::V2::CommentsController#index as application/vnd.rails5_api_test.v1
Comment Load (0.6ms) SELECT "comments".* FROM "comments"
[active_model_serializers] User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::JsonApi (4.32ms)
Completed 200 OK in 7ms (Views: 5.0ms | ActiveRecord: 1.5ms)
这是我的工作文件:
约束文件,lib/api_constraints.rb:
class APIConstraints
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(req)
req.headers["Accept"].include?(media_type) || @default
end
private
def media_type
"application/vnd.rails5_api_test.v#{@version}"
end
end
路由文件,config/routes.rb:
Rails.application.routes.draw do
require "api_constraints"
scope module: 'api/v1', constraints: APIConstraints.new(version: 1) do
resources :users
end
scope module: 'api/v2', constraints: APIConstraints.new(version: 2, default: true) do
resources :users
resources :comments
end
end
v1 的用户控制器,api/v1/users_controller.rb:
class Api::V1::UsersController < ApplicationController
def index
@users = User.all
render json: @users, each_serializer: ::V1::UserSerializer
end
end
v2 的用户控制器,api/v2/users_controller.rb:
class Api::V2::UsersController < Api::V1::UsersController
def index
@users = User.all
render json: @users, each_serializer: ::V2::UserSerializer
end
end
v2 的评论控制器,api/v2/comments_controller.rb:
class Api::V2::CommentsController < ApplicationController
def index
@comments = Comment.all
render json: @comments, each_serializer: ::V2::CommentSerializer
end
end
v1 的用户序列化器,user_serializer.rb:
class V1::UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
end
v2 的用户序列化程序,user_serializer.rb:
class V2::UserSerializer < V1::UserSerializer
has_many :comments
end
v2 的评论序列化程序,comment_serializer.rb:
class V2::CommentSerializer < ActiveModel::Serializer
attributes :id, :description
belongs_to :user
end
我尝试删除路由中的 default: true
选项,然后它按预期工作。但我希望它使用默认选项。
任何人都可以让我知道我哪里错了,并分享您对这种方法的看法。如果这不是最好的方法,请指导我完成正确的实施方法。提前感谢任何花时间帮助我的人。 :) 干杯!
我不认为这可以轻易解决,因为 v1 没有注释 v2 无论如何都会被匹配。
你APIConstraints用的是这个方法吗?
def matches?(req)
@default || req.headers['Accept'].include?("application/vnd.example.v#{@version}")
end
我认为这里的方法有点过于宽松,应该像这样忽略确实有版本的请求。
def matches?(req)
(@default &&
req.headers['Accept'].grep(/^application/vnd.example.v\d+/$).empty?
) || req.headers['Accept'].include?("application/vnd.example.v#{@version}")
end
我正在尝试基于 Railscasts #350 episode 实现一个简单的 rails api 带版本控制的应用程序。我想通过 Accept header
以这种格式提供版本来访问应用程序的特定 API 版本:application/vnd.rails5_api_test.v1
。当没有提供 Accept header
时,请求将被路由到应用程序的当前默认版本。为了处理这个问题,我在我的 lib directory
中创建了一个 api_constraints
文件,这是路由中所必需的。
我创建了两个版本的应用 v1
和 v2
,其中 v1
有 Users resource
和 v2
有 Users and Comments resources
.
一切都按预期工作,除了当我使用 Postman 通过 headers 传递版本 1
来请求 URL localhost:3000/comments
时,我从 comments resource
, 显示所有评论。但我希望响应是 status: 404 Not Found
,因为 comments resource
的版本是 2
,而请求的版本是 1
。
这是服务器的响应:
Started GET "/comments" for 127.0.0.1 at 2016-04-01 20:57:53 +0530
Processing by Api::V2::CommentsController#index as application/vnd.rails5_api_test.v1
Comment Load (0.6ms) SELECT "comments".* FROM "comments"
[active_model_serializers] User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::JsonApi (4.32ms)
Completed 200 OK in 7ms (Views: 5.0ms | ActiveRecord: 1.5ms)
这是我的工作文件:
约束文件,lib/api_constraints.rb:
class APIConstraints
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(req)
req.headers["Accept"].include?(media_type) || @default
end
private
def media_type
"application/vnd.rails5_api_test.v#{@version}"
end
end
路由文件,config/routes.rb:
Rails.application.routes.draw do
require "api_constraints"
scope module: 'api/v1', constraints: APIConstraints.new(version: 1) do
resources :users
end
scope module: 'api/v2', constraints: APIConstraints.new(version: 2, default: true) do
resources :users
resources :comments
end
end
v1 的用户控制器,api/v1/users_controller.rb:
class Api::V1::UsersController < ApplicationController
def index
@users = User.all
render json: @users, each_serializer: ::V1::UserSerializer
end
end
v2 的用户控制器,api/v2/users_controller.rb:
class Api::V2::UsersController < Api::V1::UsersController
def index
@users = User.all
render json: @users, each_serializer: ::V2::UserSerializer
end
end
v2 的评论控制器,api/v2/comments_controller.rb:
class Api::V2::CommentsController < ApplicationController
def index
@comments = Comment.all
render json: @comments, each_serializer: ::V2::CommentSerializer
end
end
v1 的用户序列化器,user_serializer.rb:
class V1::UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
end
v2 的用户序列化程序,user_serializer.rb:
class V2::UserSerializer < V1::UserSerializer
has_many :comments
end
v2 的评论序列化程序,comment_serializer.rb:
class V2::CommentSerializer < ActiveModel::Serializer
attributes :id, :description
belongs_to :user
end
我尝试删除路由中的 default: true
选项,然后它按预期工作。但我希望它使用默认选项。
任何人都可以让我知道我哪里错了,并分享您对这种方法的看法。如果这不是最好的方法,请指导我完成正确的实施方法。提前感谢任何花时间帮助我的人。 :) 干杯!
我不认为这可以轻易解决,因为 v1 没有注释 v2 无论如何都会被匹配。
你APIConstraints用的是这个方法吗?
def matches?(req)
@default || req.headers['Accept'].include?("application/vnd.example.v#{@version}")
end
我认为这里的方法有点过于宽松,应该像这样忽略确实有版本的请求。
def matches?(req)
(@default &&
req.headers['Accept'].grep(/^application/vnd.example.v\d+/$).empty?
) || req.headers['Accept'].include?("application/vnd.example.v#{@version}")
end