I have a java code which serves http endpoints using SparkJava ( http://sparkjava.com/ ).
Endpoints are generated like that:
get("/xxx/yyy", (request, response) -> {
...
return SOMETHING_TO_RETURN
});
With that approach I have a problem:
let's say I have an endpoint defined: '/api/status/car',
but users sometimes invoke '/api/status/ CAR ' instead. So the problem is with case sensitive of url defined like that.
Now I have to fix it somehow: make that case insensitive. I had take a look on filters (eg 'before'), but I can't modify request url (toLowerCase) I believe.
So the main question is: With defining endpoints using that approach, how can I modify request url to be lowercase, or to say sparkjava that urls should be mapped with case insensitive mode.
URLs (except the domain name part) might always be case-sensitive. It's up to the server to decide and therefore the user can never know. You can read about it more in W3.org .
One approach to solve your problem could be using request params :
get("/api/status/:carParam", (request, response) -> {
if (request.params(":carParam").equalsIgnoreCase("car")) {
return SOMETHING_TO_RETURN;
}
});
If you have more routes under /api/status/
except car
then you should rename :carParam
to a more generic name like :param
and then inside the handler body, you'd check this query param and return the right return value accordingly. For example:
get("/api/status/:param", (request, response) -> {
if (request.params(":param").equalsIgnoreCase("car")) {
return SOMETHING_TO_RETURN_CAR;
} else if (request.params(":param").equalsIgnoreCase("passenger")) {
return SOMETHING_TO_RETURN_PASSENGER;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.