Restlet, An example of a get with parameters

左心房为你撑大大i 提交于 2019-12-06 08:49:36

A good approach with REST is to specify this contact id within the URI. Something like that: /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).

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 no 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"); allows you extract details from the URL.

Hope this helps

Richard Berger

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? for a similar approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!