【发布时间】:2011-10-12 21:21:57
【问题描述】:
我正在尝试在 Restlet 中做一个简单的帖子。
发生的事情是我单击了 index.html 中显示的链接,当我转到服务器输出时,它显示我正在点击 url,但它从未进入我的 @Post 方法。我似乎正确地遵循了教程,但显然这里有问题。
如果我将内容更改为获取而不是发布,我可以在 URL 中使用它们,并且效果很好。但我真的很想通过邮寄而不是通过获取来发送东西。
如何将 Restlet 输入到我的后期过程中?
更新 1:
由于看起来我为参数传递的数据是一个 json 对象,我尝试将 @Post 更改为 @Post("json") 但它没有做任何更改...
下面是相关部分的代码转储,因为我确信其中至少有一个会引起人们的兴趣。我将对此进行跟踪以解决后续问题。谢谢!
开始代码转储:
这是我的“index.html”,无耻地改编自 jquery 教程(为我的 javascript 体验提供一个指标)。
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function doit() {
$(document).ready(function() {
$.post(
"http://localhost:15627/system/create",
{ key: "something here", auth: "happy", name: "The name" },
function(data) {
alert("Response: " + data);
}
);
});
}
</script>
</head>
<body>
<a href="javascript:doit()">Link</a>
</body>
</html>
现在在我的 Java restlet 中,我有一个非常简单的设置
public static void main(String[] args) throws Exception {
Component component = new Component();
component.getServers().add(Protocol.HTTP, 15627);
startDatabase();
Application main = new Main();
component.getDefaultHost().attachDefault(main);
component.start();
}
private Engine engine;
public Main() {
this.engine = new Engine();
}
还有我的入站根
public Restlet createInboundRoot() {
// Create a root router
Router router = new Router(getContext());
router.attach("/system/knock", new Knock(engine));
router.attach("/system/create", new CreatePlayer(engine));
return router;
}
还有我的创建播放器例程
@Post
public Representation create(Representation rep) {
Representation response = null;
Form form = new Form(rep);
String key = form.getFirstValue("key");
String auth = form.getFirstValue("auth");
String name = form.getFirstValue("name");
System.out.println("Creating " + key + " " + auth + " " + name);
String result = engine.create(key,auth,name);
response = new StringRepresentation(result,MediaType.TEXT_PLAIN);
return response;
}
通话结果
Oct 12, 2011 3:32:23 PM org.restlet.engine.log.LogFilter afterHandle
INFO: 2011-10-12 15:32:23 127.0.0.1 - - 15627 OPTIONS /system/create - 200 0 0 0 http://localhost:15627 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729) -
冰壶(遵循此处的建议Restlet POST using JSON
C:\programs\curl>curl -X POST localhost:15627/system/create -H "Content-Type: ap
plication/json" -d '{"key" : "something", "auth" : "happy", "name" : "The name"}
'
curl: (7) couldn't connect to host
curl: (6) Could not resolve host: something,; Host not found
curl: (6) Could not resolve host: auth; Host not found
curl: (7) couldn't connect to host
curl: (6) Could not resolve host: happy,; Host not found
curl: (6) Could not resolve host: name; Host not found
curl: (7) couldn't connect to host
curl: (3) [globbing] unmatched close brace/bracket at pos 9
C:\programs\curl>
以及 curl 的 restlet 输出
Oct 12, 2011 3:43:47 PM org.restlet.engine.log.LogFilter afterHandle
INFO: 2011-10-12 15:43:47 127.0.0.1 - - 15627 POST /system/create - 200 0 5 0 http://localhost:15627 curl/7.22.0 (i386-pc-win32) libcurl/7.22.0 OpenSSL/0.9.8r zlib/1.2.5 -
更新 2:
我添加了一个 handle(Request req, Response res) 方法,当我尝试 curl 时,它当前正在响应获取和发布。在任何一种情况下,它都无法提取与其关联的任何数据。
【问题讨论】:
-
2 条建议:1) 发布您的 Restlet 应用程序的输出,以便我们可以在您进行 POST 调用时看到它正在记录的内容(可能是 404'ing),以及 2) 使用 @987654333 @ 从命令行手动进行 POST 调用(这至少会从等式中删除 jQuery。像这样将问题分成两半将帮助您确定它是 Restlet 还是 jQuery 问题。
-
@Brian 感谢您的回复。我已经添加了调用的结果。至于冰壶,我在Windows系统上。有什么想法我可以如何做到这一点?老实说,我不关心 jquery - 我的 END 应用程序将在 flash 或 java 中。在这一点上,我只关心服务器端的事情。所以如果有比使用jQuery更好的测试方法,我宁愿使用它!
-
@Brian 我已经添加了
curl的结果。 -
由于命令行中的引号字符,您的 curl 命令被占用。您应该将 JSON 放入一个文件并使用 -F 或 -d 选项(以及指定源文件名的 @ 符号)将其传递。有关详细信息,请参阅curl.haxx.se/docs/manpage.html#-F。