0

I'm developing a REST API using Restlet.

So far everything has been working just fine. However, I now encountered an issue with the Router mapping of URL to ServerResource.

I've got the following scenario:

  • GET /car returns a list of all cars
  • GET /car/{id} returns details about the car with id 1
  • GET /car/advancedsearch?param1=test should run a search across all cars with some parameters

The first two calls work without any problems. If I try to hit the third call though, the Restlet Router somehow maps it to the second one instead. How can I tell Restlet to instead use the third case?

My mapping is defined as follows:

router.attach("/car",                CarListResource.class);
router.attach("/car/{id}",           CarResource.class);
router.attach("/car/advancedsearch", CarSearchResource.class);

CarSearchResource is never invoked, but rather the request ends up in CarResource.

The router's default matching mode is set to Template.MODE_EQUALS, so that can't be causing it.

Does anyone have any further suggestions how I could fix it?


Please don't suggest to use /car with the parameters instead, as there's already another kind of search in place on that level. Also, I'm not in control of the API structure, so it has to remain as it is.

Baz
  • 36,440
  • 11
  • 68
  • 94

2 Answers2

0

you need to add .setMatchingQuery(true); to that rout in order it to recognize that it is with a query at the end of it.

    Router router = (Router) super.createInboundRoot();
    TemplateRoute route1 = router.attach("/car/advancedsearch?{query_params}", MyResource.class);
    route1.setMatchingQuery(true);
    return router;

Mind that this pattern is with the exact specific order that you have determined in the route i.e. advancedsearch comes first and query_params comes after

Igor
  • 846
  • 1
  • 11
  • 25
  • I did not have to do that for the main `/car` path and that one uses parameters as well. Also, it has to be `/car/advancedsearch`, not `/advancedsearch`. – Baz Jul 20 '16 at 06:13
  • I've also tried this now, but the result is the same. – Baz Jul 20 '16 at 08:24
  • its only true for query form – Igor Jul 20 '16 at 11:02
  • 1
    I'm sorry, but I don't understand your comment. `/car` and `/car/advancedsearch` *both* have query parameters in my case. The first works just fine now, the second doesn't. – Baz Jul 20 '16 at 11:06
0

I was able to solve this by simply reordering the attach statements:

router.attach("/car/advancedsearch", CarSearchResource.class);
router.attach("/car",                CarListResource.class);
router.attach("/car/{id}",           CarResource.class);
Baz
  • 36,440
  • 11
  • 68
  • 94