Spring云api网关没有正确发送POST请求



我有两个应用

  • 。单体(使用java servlet开发)
  • b。Spring启动微服务(开发中)

我正在尝试开发spring云api网关,它将位于这两个应用程序的前面,并将根据它收到的请求为流量提供服务。大多数情况下,这两个应用程序都支持POST方法和api网关中的路由,对于b.spring引导微服务来说工作得很好,但是当我在单体上尝试相同的请求和url时,观察到spring云api网关发送GET请求(这是不允许的)并以404错误失败。是否有任何方法来确定到底是什么请求api网关尝试?还是api网关先尝试GET,然后POST ?

通过检查访问日志

,我能够确认我在api网关上尝试的POST请求上收到GET请求。
0:0:0:0:0:0:0:1 - - [14/Jul/2022:12:34:31 -0700] "**GET** /myservice/revers HTTP/1.1" 404 1685 "PostmanRuntime/7.29.0" "0/455" 

在启用TRACE 后,从api网关获取的日志

2022-07-15 01:04:30.426  INFO 16016 --- [ctor-http-nio-3] c.t.tes.gwapi.config.LoggingFilter   : Incoming request http://localhost:8080/servlet ,is routed uri:http://10.2.1.55:3068/myservice/revers with id: my-route-id
2022-07-15 01:04:31.665 DEBUG 16016 --- [ctor-http-nio-3] r.netty.http.client.HttpClientConnect    : [d95e4f2a-1, L:/10.01.57.01:59773 - R:/10.2.1.55:3068] Handler is being applied: {uri=http://10.2.1.55:3068/myservice/revers , method=POST}
2022-07-15 01:04:31.974 DEBUG 16016 --- [ctor-http-nio-3] r.n.http.client.HttpClientOperations     : [d95e4f2a-1, L:/10.01.57.01:59773 - R:/10.2.1.55:3068] Received response (auto-read:false) : [Date=Thu, 14 Jul 2022 19:34:31 GMT, Server=Apache, Last-Modified=Wed, 06 Jul 2022 09:15:29 GMT, Accept-Ranges=bytes, Content-Type=text/html, content-length=1685]
2022-07-15 01:04:31.983 DEBUG 16016 --- [ctor-http-nio-3] r.n.http.client.HttpClientOperations     : [d95e4f2a-1, L:/10.01.57.01:59773 - R:/10.2.1.55:3068] Received last HTTP packet
2022-07-15 01:04:31.987  INFO 16016 --- [ctor-http-nio-3] c.t.t.gwapi.config.SpringCloudConfig     : Response body <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
sample html 
下面是示例api config


@Bean
public RouteLocator myRouteSavingRequestBody(RouteLocatorBuilder builder) {
return builder.routes()
.route("my-route-id",
p -> p
.path("/servlet/**") //your own path filter
.filters(f -> f
.modifyResponseBody(String.class, String.class,
(webExchange, originalBody) -> {
if (originalBody != null) {
//log.info("Response body {}", originalBody);
return Mono.just(originalBody);
} else {
return Mono.empty();
}
})
.modifyRequestBody(String.class, String.class,
(webExchange, originalBody) -> {
if (originalBody != null) {
// log.info("Request body {}", originalBody);
return Mono.just(originalBody);
} else {
return Mono.empty();
}
})
.setPath("/myservice/revers")
)
.uri("http://10.2.1.55:3068")
)
.build();
}

Nginx重定向可能导致请求方法改变。我使用RouteLocator作为网关将POST请求重定向到HTTPS url,但我用HTTP url写了一个错误的uri,因此Nginx重定向到HTTPS并发送GET请求而不是POST请求。

最新更新