"TypeError: object is not callable" using `cherrypy.dispatch.MethodDispatcher()`

"TypeError: object is not callable" using `cherrypy.dispatch.MethodDispatcher()`

我正在学习 cherrypy 教程 "Give it a REST",除了我想让我的 cherrypy 服务器启动两个 类:一个用于提供一些静态文件,另一个用于 RESTful API:

api.py:

import cherrypy

class TestApi(object):
    conf = {
        '/api/v1/test': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        }
    }
    exposed = True

    def GET(self):
        return "Test GET!"

server.py:

import cherrypy

import api

server_conf = {
    'server.socket_port': 1313,
}


class Root(object):
    conf = {
        '/': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': "/some/path",
            'tools.staticdir.debug': True
        }
    }

    @cherrypy.expose
    def index(self):
        return "Hello world!"


if __name__ == '__main__':
    cherrypy.config.update(server_conf)
    cherrypy.tree.mount(Root(), '/', Root.conf)

    cherrypy.tree.mount(api.TestApi(), '/api/v1/test',
                        api.TestApi.conf)
    cherrypy.engine.start()
    cherrypy.engine.block()

但是,当我启动服务器 (python server.py) 并在 http://localhost:1313/api/v1/test 上执行 GET 时,我收到此错误:

500 Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

Traceback (most recent call last): File "/usr/local/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 670, in respond response.body = self.handler() File "/usr/local/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 217, in call self.body = self.oldhandler(*args, **kwargs) File "/usr/local/lib/python2.7/site-packages/cherrypy/_cpdispatch.py", line 68, in call raise x TypeError: 'TestApi' object is not callable

我查找了类似的问题并发现了 how to use multiple dispatchers in same cherrypy application?,但不清楚那里的答案是否真的适用于我。任何指针将不胜感激!

刚刚意识到问题出在 TestApi.conf:

需要在下面的部分中将配置的路径从 '/api/v1/test' 更改为 '/'

class TestApi(object):
    conf = {
        '/api/v1/test': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        }
    }

我想这是因为我已经在 server.py 中传入了挂载路径,所以从那时起配置路径是相对的。