micronaut将httprequest重定向到不同的服务
micronaut中有声明式客户端:micronaut将httprequest重定向到不同的服务,micronaut,micronaut-client,Micronaut,Micronaut Client,micronaut中有声明式客户端: @Client("http://localhost:5000") public interface ServiceB { @Get("/ping") HttpResponse ping(HttpRequest httpRequest); } 在我的controller类中,我想将传入请求重定向到ServiceB @Controller("/api") public class ServiceA { @Inject pri
@Client("http://localhost:5000")
public interface ServiceB {
@Get("/ping")
HttpResponse ping(HttpRequest httpRequest);
}
在我的controller
类中,我想将传入请求重定向到ServiceB
@Controller("/api")
public class ServiceA {
@Inject
private ServiceB serviceB;
@Get("/ping)
HttpResponse pingOtherService(HttpRequest httpRequest){
return serviceB.ping(httpRequest)
}
}
然而,由于请求中编码的信息,
ServiceB
似乎永远不会得到请求。如何将请求从ServiceA
转发到ServiceB
?客户端无法直接发送和HttpRequest。他将根据客户机的参数构建一个
我试图在客户端的主体中发送重定向请求,但出现堆栈溢出错误:jackson无法将其转换为字符串
您不能更改请求中的URI以将其发送回,不幸的是,没有任何HttpRequest实现在URI上有setter
如果确实要发送完整请求(头、体、参数…),可以尝试配置代理
否则,如果您不必通过完整请求,您可以通过客户端传递您需要的内容:
客户示例:
@Client("http://localhost:8080/test")
public interface RedirectClient {
@Get("/redirect")
String redirect(@Header(value = "test") String header);
}
控制员:
@Slf4j
@Controller("/test")
public class RedirectController {
@Inject
private RedirectClient client;
@Get
public String redirect(HttpRequest request){
log.info("headers : {}", request.getHeaders().findFirst("test"));
return client.redirect(request.getHeaders().get("test"));
}
@Get("/redirect")
public String hello(HttpRequest request){
log.info("headers : {}", request.getHeaders().findFirst("test"));
return "Hello from redirect";
}
}
我为一个头做了这个,但是你可以用一个body(如果不是GET方法的话)、请求参数等等来做。我认为客户端将调用
ServiceA
中的pingOtherService
方法,你将在那里处于无限循环中。我从问题中假设ServiceA
在一个应用程序中,客户端应该调用另一个应用程序。你说得对,这与URI无关,而是与客户端无法传递的请求有关。我已经编辑了答案。