【问题标题】:Restlet not postingRestlet不发布
【发布时间】: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

标签: jquery post restlet


【解决方案1】:

一些事情:您的内容类型是 JSON,但在您尝试创建表单表示的服务器上。您需要通过使用 @Post 函数的 rep 参数进行初始化来使用 JSONRepresentation 类

为什么您会根据 Restlet 日志收到 200 响应:可能是因为所有表单值都是空/null,并且可能是您的创建播放器正在使用空值 - 检查一下,您会看到它可能就这样吧。

是的,@Post("json") 很好,但如果您不将传入的表示形式处理为 JSON...

什么是未知的:你甚至在你的警报中得到任何回应吗?返回的值是多少?未定义??

为了您的利益,请尝试使用$.ajax 并明确说明,您可能会意识到您可能在哪里步履蹒跚,有时速记会导致一些错误...一旦您熟练使用它,它将有助于使用短手。我从来没有能够正确地用速记进行思考,所以我更喜欢代码的统一性——没有$.delete$.put,所以我只使用$.ajax(这仅供参考,与问题...)

试试这些,看看它是否有效......

【讨论】:

    【解决方案2】:

    所以我最终让它工作了。我需要使用 ServerResource,而不是使用 Restlet。这是我的解决方案

    public class CreatePlayer extends ServerResource {
    
        private Engine engine; 
    
        public CreatePlayer() {
            this(Main.getEngine());
        }
    
        public CreatePlayer(Engine engine) {
            this.engine = engine;
        }
    
        @Post
        public Representation create(Representation rep) {
            System.err.println("Inside create");
            Form form = new Form(rep);
    
            String key = form.getFirstValue("key");
            String auth = form.getFirstValue("auth");
            String name = form.getFirstValue("name");
    
            System.err.println("Creating " + key + " " + auth + " " + name);
    
            String result = engine.create(key,auth,name);
    
            return new StringRepresentation(result,MediaType.TEXT_PLAIN);
        }
    
    }
    

    还有我的新入站根:

    public Restlet createInboundRoot() {
        // Create a root router
        Router router = new Router(getContext());
    
        router.attach("/homage/knock", Knock.class);
        router.attach("/homage/create", CreatePlayer.class);
    
        return router;
    }
    

    更新 3:

    为了测试它,我从 jquery 和 curl 切换,并使用 java 测试驱动程序。当我这样做时,我能够得到所有东西,除了我的驱动程序,无论出于何种原因,无法产生多个值。所以如果我这样做name=foo&amp;auth=happy" it gives me a variablenameand a value offoo&auth=happy`。所以这显然是我的驱动程序没有正确发布的工件,这对我来说已经足够好了。我相信事物的前端将能够制作格式正确的帖子。如果一切都失败了,我可以破解它以将我需要的任何变量合二为一并将其拆分。不太理想,但它现在可以工作。

    感谢所有帮助,伙计们!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多