Apache2 :: REST - 创建资源

时间:2015-10-09 21:11:15

标签: perl rest

我是REST的新手,我是Perl的Apache2 :: REST的新手。我安装了'Apache2 :: REST',我正在阅读它的食谱。 我知道'REST'正在发挥作用,因为当我去http://localhost/api时,我得到了正确的回应:

response message="" status="200">
<data api_mess="Hello, this is MyApp REST API"/>
</response>

以下是MyAPP :: REST :: API

的代码
package MyApp::REST::API;
use warnings;
use strict;

use base qw/Apache2::REST::Handler/;

# Implement the GET HTTP method.
sub GET{
 my ($self, $request, $response) = @_;
 $response->data()->{'api_mess'} = "Hello, this is MyApp REST API";
 return Apache2::Const::HTTP_OK;
}

# Authorize the GET method.
sub isAuth{my ($self, $method, $req) = @_; return $method eq "GET";} 
1;

我准备创建一个'资源'所以,我想创建一个资源'/ foo'。

该食谱说“实现一个名为MyApp :: REST :: API :: foo的Apache2 :: REST :: Handler”

我去了perldoc文档,我真的无法理解。

有人可以解释一下。

1 个答案:

答案 0 :(得分:0)

Apache2 :: REST模块中包含演示安装。当您在没有任何Apache2RESTHandlerRootClass设置的情况下配置Apache2 :: REST mod_perl2处理程序时(查看模块或apache配置的cpan文档),将在API访问点自动安装演示安装。

我查看了演示安装提供的文件,它给了我创建资源的提示。

例如: 对于名为“test”的资源,请使用此网址http://localhost/test/。文件位于(这是Debian) /usr/local/share/perl/5.20.2/Apache2/REST/Handler/test.pm。

对于名为'/ foo'的资源,我只是复制了test.pm并在foo.pm中调用。

请参阅https://code.google.com/p/apache2rest/wiki/DemoApplication