简体   繁体   English

Restlet,带参数的 get 示例

[英]Restlet, An example of a get with parameters

Just startred using restlet with java and was pleasently surprised how easy it was.刚开始在 java 中使用 restlet,并惊讶地发现它是如此简单。 However this was with puts.然而,这是看跌期权。 I then started to work with get but couldn't work out how to pass infromation with the get.然后我开始使用 get 但无法弄清楚如何使用 get 传递信息。

With the put it was easy as:使用 put 很容易,因为:

@Put
public Boolean store(Contact contact);

But when i try and do this with get it doesnt work.但是当我尝试使用 get 执行此操作时,它不起作用。 From reading around i think i have to not pass it any parameters and just have this:通过阅读,我认为我不必传递任何参数,只需要有这个:

@Get
public Contact retrieve();

and then pass the parameters in a url or something?然后在 url 或其他东西中传递参数? But i cant find any info on how to do this.但我找不到有关如何执行此操作的任何信息。 As with put i could just use:与 put 一样,我可以使用:

resource.store(user1);

Any help please?请问有什么帮助吗?

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.如何从另一端的 url 中获取信息的示例也将非常有帮助。

Thanks谢谢

I now have on my client side:我现在在我的客户端:

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

On the server side i have:在服务器端,我有:

@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.它在我的程序中不存在,并且不确定它需要是什么类型或在哪里声明它。

A good approach with REST is to specify this contact id within the URI. REST 的一个好方法是在 URI 中指定此联系人 ID。 Something like that: /contacts/mycontactid.类似的东西:/contacts/mycontactid。

When attaching your resources within the application class, you can define this segment as an attribute (the contact id one in your case).在应用程序类中附加您的资源时,您可以将此段定义为一个属性(在您的情况下为联系人 ID)。

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蒂埃里

I know this question was asked along time ago but the answer I think you are looking for is:我知道这个问题很久以前就有人问过,但我认为您正在寻找的答案是:

Application Code应用代码

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

Resource Code资源代码

@Get
public void login() 
String userName = (String)this.getRequestAttributes().get("user");

The (String)this.getRequestAttributes().get("user"); (String)this.getRequestAttributes().get("user"); allows you extract details from the URL.允许您从 URL 中提取详细信息。

Hope this helps希望这可以帮助

It seems that what you are looking for is something like:似乎您正在寻找的是这样的:

public final Representation get() {
  String contactId = request.getAttributes().get("contactId"));
  // Find the Contact object with that id
  JacksonRepresentation<Contact> result = 
    new JacksonRepresentation<Contact>(contact);
  return result;
}

Also see: how to pass parameters to RESTlet webservice from android?另请参阅: 如何将参数从 android 传递给 RESTlet webservice? for a similar approach.对于类似的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM