Restlet,带参数的 get 示例 [英] Restlet, An example of a get with parameters

查看:51
本文介绍了Restlet,带参数的 get 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始在 java 中使用 restlet,并惊喜地发现它是如此简单.然而,这是看跌期权.然后我开始使用 get 但无法弄清楚如何使用 get 传递信息.

Just startred using restlet with java and was pleasently surprised how easy it was. However this was with puts. I then started to work with get but couldn't work out how to pass infromation with the get.

有了 put 就很简单了:

With the put it was easy as:

@Put
public Boolean store(Contact contact);

但是当我尝试使用 get 执行此操作时,它不起作用.通过阅读,我认为我不必向它传递任何参数,只需要有这个:

But when i try and do this with get it doesnt work. From reading around i think i have to not pass it any parameters and just have this:

@Get
public Contact retrieve();

然后在url中传递参数什么的?但我找不到有关如何执行此操作的任何信息.与 put 一样,我可以使用:

and then pass the parameters in a url or something? But i cant find any info on how to do this. As with put i could just use:

resource.store(user1);

请问有什么帮助吗?

我很确定这是那种我只需要看到一个例子然后就可以轻松完成的事情.如何从另一端的 url 中获取信息的示例也会非常有帮助.

Im pretty sure this is the kind of thing i just need to see an example of and then ill be able to do it easily. Example of how to get the infromation out of the url at the other side would be very helpful aswell.

谢谢

我现在在我的客户端:

String username = "tom";
ClientResource cr2 = new ClientResource("http://.../ContactManager/contacts/" + username);
ContactResource resource2 = cr2.wrap(ContactResource.class);
resource2.logIn();

在服务器端我有:

@Get
public Contact logIn(){
    System.out.println("name is " + resource.getAttributes().get("contactId"));     
    return null;
}

但我不确定什么是资源?它在我的程序中不存在,我不确定它需要是什么类型或在哪里声明它.

But i am not sure what resource is? It doesnt exist in my program and am not sure what type it needs to be or where to declare it.

推荐答案

REST 的一个好方法是在 URI 中指定此联系人 ID.类似的东西:/contacts/mycontactid.

A good approach with REST is to specify this contact id within the URI. Something like that: /contacts/mycontactid.

在应用程序类中附加您的资源时,您可以将此段定义为一个属性(在您的情况下为联系人 ID).

When attaching your resources within the application class, you can define this segment as an attribute (the contact id one in your case).

public class ContactsApplication extends Application {
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/contacts/", ContactsServerResource.class);
        router.attach("/contacts/{contactId}", ContactServerResource.class);
        return router;
    }
}

然后你可以在他的回答中找到理查德提供的代码.

Then you can have the code provided by Richard in his answer.

希望对你有帮助.蒂埃里

Hope it helps you. Thierry

这篇关于Restlet,带参数的 get 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆