Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在筛选器中获取请求路径变量?[Micronaut 1.3.2]_Java_Micronaut - Fatal编程技术网

Java 如何在筛选器中获取请求路径变量?[Micronaut 1.3.2]

Java 如何在筛选器中获取请求路径变量?[Micronaut 1.3.2],java,micronaut,Java,Micronaut,通常在Spring中,我们可以通过以下方式检索path变量: final Map<String, String> pathVariables = (Map<String, String>) request .getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); 以下是我到目前为止所做的 控制器: @Get(value = "/{variable}/anotherpa

通常在Spring中,我们可以通过以下方式检索path变量:

final Map<String, String> pathVariables = (Map<String, String>) request
                 .getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
以下是我到目前为止所做的

控制器:

@Get(value = "/{variable}/anotherpath")
public Single<HttpResponse<ResponseESQ>> myController(String variable)  {}
过滤器:

@Filter("/**")
public class myFilter implements HttpServerFilter {
  @Override
  public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) { 
    // I need here to consume the path variable
    request.getAttribute("variable")
  }
}
我尝试使用:request.getAttributes,但它不起作用


我们如何在Micronaut中实现同样的功能?

给定以下控制器,其URI包含两个路径变量某物和名称

@控制员/说 公共类超级控制器{ @获取{something}/to/{name} @产生 公共字符串helloString某物、字符串名称{ 返回字符串。格式%s%s,某物,名称; } } 您可以编写一个过滤器,通过访问io.micronaut.http.HttpMessagegetAttributes中包含的io.micronaut.web.router.UriRouteMatch来访问路径变量

下面的示例筛选器访问路径变量

@滤器/** 公共类SuperFilter实现HttpFilter{ @凌驾 公共发布者>doFilterHttpRequest请求,过滤器链{ 可选uriRouteMatch=请求 .getAttributes .GetHttpAttribute.ROUTE_MATCH.toString,UriRouteMatch.class; 如果uriRouteMatch.isPresent{ //访问路径变量。 Map variableValues=uriRouteMatch.get.getVariableValues; System.out.PrintLnVariableValue; } return chain.proceedrequest; } }
希望这能回答你的问题。祝你好运,享受Micronaut带来的乐趣。

给定以下具有URI的控制器,该URI包含两个路径变量某物和名称

@控制员/说 公共类超级控制器{ @获取{something}/to/{name} @产生 公共字符串helloString某物、字符串名称{ 返回字符串。格式%s%s,某物,名称; } } 您可以编写一个过滤器,通过访问io.micronaut.http.HttpMessagegetAttributes中包含的io.micronaut.web.router.UriRouteMatch来访问路径变量

下面的示例筛选器访问路径变量

@滤器/** 公共类SuperFilter实现HttpFilter{ @凌驾 公共发布者>doFilterHttpRequest请求,过滤器链{ 可选uriRouteMatch=请求 .getAttributes .GetHttpAttribute.ROUTE_MATCH.toString,UriRouteMatch.class; 如果uriRouteMatch.isPresent{ //访问路径变量。 Map variableValues=uriRouteMatch.get.getVariableValues; System.out.PrintLnVariableValue; } return chain.proceedrequest; } }
希望这能回答你的问题。祝你好运,享受Micronaut带来的乐趣。

我删除了我的答案,因为你的问题特别想从请求中检索它们……我的答案没有提到。抱歉吵闹,没问题。我现在正在尝试不同的解决方案,比如:request.getAttributes,但我还没有弄明白。你可以通过执行请求绑定来使用它吗?好的,我想我需要添加一些信息。我从过滤器中尝试。一旦你成功,请为其他人分享:我删除了我的答案,因为你的问题特别想从请求中检索它们…我的答案没有解决。抱歉吵闹,没问题。我现在正在尝试不同的解决方案,比如:request.getAttributes,但我还没有弄明白。你可以通过执行请求绑定来使用它吗?好的,我想我需要添加一些信息。我从过滤器中尝试。一旦成功,请为他人分享: