如何在过滤器中获取请求路径变量? [微型机器人 1.3.2]
How to get a request path variable within a Filter? [Micronaut 1.3.2]
通常在 Spring 中,我们可以通过以下方式检索路径变量:
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
和 name
。
@Controller("/say")
public class SuperController {
@Get("{something}/to/{name}")
@Produces
public String hello(String something, String name) {
return String.format("%s %s", something, name);
}
}
您可以编写一个过滤器,通过访问 io.micronaut.http.HttpMessage#getAttributes
中包含的 io.micronaut.web.router.UriRouteMatch
来访问路径变量。
以下示例过滤器访问路径变量。
@Filter("/**")
public class SuperFilter implements HttpFilter {
@Override
public Publisher<? extends HttpResponse<?>> doFilter(HttpRequest<?> request, FilterChain chain) {
Optional<UriRouteMatch> uriRouteMatch = request
.getAttributes()
.get(HttpAttributes.ROUTE_MATCH.toString(), UriRouteMatch.class);
if (uriRouteMatch.isPresent()) {
// access the path variables.
Map<String, Object> variableValues = uriRouteMatch.get().getVariableValues();
System.out.println(variableValues);
}
return chain.proceed(request);
}
}
希望这能回答您的问题。祝你好运,玩得开心。
通常在 Spring 中,我们可以通过以下方式检索路径变量:
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
和 name
。
@Controller("/say")
public class SuperController {
@Get("{something}/to/{name}")
@Produces
public String hello(String something, String name) {
return String.format("%s %s", something, name);
}
}
您可以编写一个过滤器,通过访问 io.micronaut.http.HttpMessage#getAttributes
中包含的 io.micronaut.web.router.UriRouteMatch
来访问路径变量。
以下示例过滤器访问路径变量。
@Filter("/**")
public class SuperFilter implements HttpFilter {
@Override
public Publisher<? extends HttpResponse<?>> doFilter(HttpRequest<?> request, FilterChain chain) {
Optional<UriRouteMatch> uriRouteMatch = request
.getAttributes()
.get(HttpAttributes.ROUTE_MATCH.toString(), UriRouteMatch.class);
if (uriRouteMatch.isPresent()) {
// access the path variables.
Map<String, Object> variableValues = uriRouteMatch.get().getVariableValues();
System.out.println(variableValues);
}
return chain.proceed(request);
}
}
希望这能回答您的问题。祝你好运,玩得开心。