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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With