RestEasy @Path Question with regular expression

2019-02-21 03:00发布

问题:

Is it possible to define path with optional path variables.

like the uri below:

/app/make/{make}/model/{model}/year/{year}/mileage/{mileage}/fuelType/{fuelType}/maxPrice/{maxprice}/transmission/{transmission}/engineSize/{engineSize}

URI may be composed of any 0 or more combinations of the parameters? Is there a way to tell resteasy that all those paths are optional?

cheers.

回答1:

Why bother using path segments? If they are optional parameters then it can't be a real hierarchy so why not just use query string parameters. They work much better for this type of parameter.



回答2:

@Path("/make/{make}{model:(/model/[^/]+)?}{fuel : (/fuel/[^/]+)?}{gearbox : (/gearbox/[^/]+)?}/cars")



app/{make:(/make/[^/]+)?}{model:(/model/[^/]+)?}{year:(/year/[^/]+)?}{mileage:(/mileage/[^/]+)?}

I came up with the above workaround which works but inside the method I need to remove the pathname.