Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple get methods for single restlet resource

Tags:

java

rest

restlet

I have the following two queries:

/api/resource/{id}
/api/resource?id=x&id=y&id=z

They need to be served with two different resource methods:

@Get
public DTO getSingleDTO();
@Get
public List<DTO> getMultipleDTO();

Is it possible to put these two methods into a single restlet resource? I tried using @Get("?id"), but this annotation isn't really powerful and not usable in my case.

What's the best way to implement this? I don't want to implement two controllers for every resource I have.

like image 480
maja Avatar asked May 14 '26 16:05

maja


1 Answers

I don't know your model, but from what I'm seeing, I'd go with single route and single resource method:

in the Application class:

router.attach("/api/resource/{id_1}?id_2=y&id_3=z", MyResource.class)

In the MyResource class:

   public Representation doMyThing() {

      String id1 = getRequestAttributes("id_1").toString()getRequestAttributes();
      String id2 = getQueryValue("id_2");
      String id3 = getQueryValue("id_3");

      if (StringUtils.isEmpty(id2) && StringUtils.isEmpty(id3)) {
        DTO result = service.handleSingleId(id1);
        return JsonRepresentation(result);
      } else {
        List<DTO> result = service.handleMultipleIds(id1, id2, id3);
        return JsonRepresentation(result);
      }     
   }

you can always return some other Representation and not JsonRepresentation as I suggested

like image 131
Igor Avatar answered May 16 '26 05:05

Igor