简体   繁体   English

SparkJava - 区分大小写的端点

[英]SparkJava - Case sensitive endpoints

I have a java code which serves http endpoints using SparkJava ( http://sparkjava.com/ ).我有一个 java 代码,它使用 SparkJava ( http://sparkjava.com/ )为 http 端点提供服务。

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',假设我定义了一个端点:'/api/status/car',
but users sometimes invoke '/api/status/ CAR ' instead.但用户有时会调用 '/api/status/ CAR '。 So the problem is with case sensitive of url defined like that.所以问题在于这样定义的 url 区分大小写。

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.我看过过滤器(例如“之前”),但我相信我无法修改请求 url(toLowerCase)。

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.所以主要问题是:通过使用这种方法定义端点,我如何将请求 url 修改为小写,或者说 sparkjava 应该使用不区分大小写的模式映射 url。

URLs (except the domain name part) might always be case-sensitive. URL(域名部分除外)可能始终区分大小写。 It's up to the server to decide and therefore the user can never know.由服务器决定,因此用户永远无法知道。 You can read about it more in W3.org .您可以在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.如果您在/api/status/除了car下还有更多路线,那么您应该将:carParam重命名为更通用的名称,例如:param ,然后在处理程序主体中,您将检查此查询参数并相应地返回正确的返回值。 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;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM