Restlet 未发帖
我正在尝试在 Restlet 中发表一篇简单的文章。
发生的情况是,我单击了显示在我的index.html 中的链接,当我转到服务器输出时,它显示我正在点击网址,但它永远不会进入我的@Post 方法。我似乎正确地遵循了教程,但显然这里出了问题。
如果我将内容更改为 get 而不是 post,我可以在 URL 中使用它们,并且效果很好。但我真的很想通过邮寄而不是获取来发送东西。
如何将 Restlet 输入到我的 post 过程中?
更新 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) -
(遵循此处找到的建议使用 JSON 的 Restlet POST
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更新2的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 -
:
我添加了一个handle(Request req, Response res)
方法,当我尝试卷曲时,它当前正在响应获取和发布,在任何一种情况下,它都无法提取与其关联的任何数据。
I'm attempting to do a simple post in Restlet.
What's happening is I'm clicking on the link that shows up in my index.html, and when I go to my server output, it shows that I'm hitting urls, but it's never entering my @Post method. I seem to be following the tutorials correctly, but obviously something is amiss here.
If I change things to get instead of post, I can use them in the URL and it works just fine. But I'd really like to send things through post instead of get.
How do I make Restlet feed into my post procedure?
Update 1:
Since it appears the data I'm passing in for parameters is a json object, I tried changing the @Post
to @Post("json")
but it didn't make any change...
Below is a code dump of relevant pieces, since I'm sure at least one of them will be of interest. I'll be tracking this for followup questions. Thanks!
Begin code dump:
Here's my "index.html", shameless adapated from the jquery tutorial (to give an indicator to my javascript experience).
<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>
Now in my Java restlet, I have a very simple setup
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();
}
And my inbound root
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;
}
And my create player routine
@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;
}
Result of a call
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) -
Curling (following advice found here 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>
And the restlet output from the curl
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 -
Update 2:
I added a handle(Request req, Response res)
method and that is currently responding to both gets and posts when I try to curl. In either case it isn't able to pull any data associated with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事:您的内容类型是 JSON,但在服务器上您正在尝试创建表单表示形式。您需要使用 JSONRepresentation 类,通过使用
@Post
函数的rep
参数进行初始化为什么根据 Restlet 日志,您会收到 200 响应:可能是因为所有表单值是空/空,并且您的创建播放器可能正在使用空值 - 检查这一点,您会发现可能是这种情况。
是的,
@Post("json")
很好,但如果您不将传入的表示形式处理为 JSON,则毫无用处...未知的情况:您是否在警报中收到任何响应?返回的值是多少?不明确的??
只是为了您的利益,尝试使用
$.ajax
并使事情变得明确,您可能会意识到您可能会犹豫不决的地方,有时速记会短路一些错误......一旦您熟练了,它就可以了将有助于使用短手。我从来都无法正确地用速记方式思考,所以我更喜欢代码的统一性 - 没有$.delete
或$.put
所以我只使用 < code>$.ajax (这仅供参考,与问题无关...)尝试一下这些,看看它是否有效...
A few things: Your Content-type is JSON but on the server you are trying to create a Form Representation. You need to use JSONRepresentation class for that by initializing with the
rep
argument of the@Post
functionWhy you are getting a 200 response as per the Restlet log: Probably because all the form values are empty/null and it may be that your create player is working with null values - check for that and you'll see it may be the case.
Yes,
@Post("json")
is good but of no use if you are not processing your incoming representation as JSON...What is unknown: Are you even getting any response in your alert? What is the value that is being returned? undefined??
Just for your benefit try using
$.ajax
and make things explicit and you'll probably realize where you may be faltering, sometimes the shorthand short circuits some errors...once you are adept at it, it'll help using the short hands. I for one have never been able to think in shorthand properly and more so I prefer uniformity in code - there is no$.delete
or$.put
so I just use$.ajax
(This is only for information, nothing to do with the problem...)Try these out and see if it works...
所以我最终让它发挥作用。我需要使用 ServerResource,而不是使用 Restlet。这是我的解决方案
和我的新入站根:
更新3:
为了测试它,我放弃了jquery和curl,并使用了java测试驱动程序。当我这样做时,我能够获取所有内容,但我的驱动程序由于某种原因无法生成多个值。因此,如果我这样做
name=foo&auth=happy" 它会给我一个变量
name和一个值
foo&auth=happy`。所以这显然是一个工件我的驱动程序没有正确发布,这对我来说已经足够了,我相信前端能够制作格式正确的帖子,如果其他方法都失败,我可以破解它以将我需要的任何变量放入其中。不太理想,但是。 我感谢所有的帮助,伙计们!
So I ended up getting it to work. Instead of using a Restlet, I needed to use a ServerResource. Here's my solution
And my new inbound root:
update 3:
To test it, I switched away from jquery, and from curl, and went with a java test driver. I'm able to get everything in when I do this, except that my driver, for whatever reason, isn't able to make multiple values. So if I do
name=foo&auth=happy" it gives me a variable
nameand a value of
foo&auth=happy`. So this is obviously an artifact of my driver not posting properly, which is good enough for me. I'm confident that the front end side of things will be capable of making a properly formatted post. If all else fails, I can hack it to put any variables I need into one and split it up. Less desirable, but it would work for now.I appreciate all the help, folks!