spring cloud gateway支持restful调用

This commit is contained in:
tanghc
2019-10-10 12:01:13 +08:00
parent 2f4c46dc16
commit fe0d348b41
5 changed files with 95 additions and 8 deletions

View File

@@ -0,0 +1,52 @@
package com.gitee.sop.gateway.controller;
import com.gitee.sop.gatewaycommon.param.ParamNames;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import java.net.URI;
/**
* @author tanghc
*/
@Configuration
public class RestWebFlux {
@Value("${sop.restful.path:/rest}")
private String restPath;
/**
* 307 Temporary Redirect临时重定向:
* <p>
* 在这种情况下请求应该与另一个URI重复但后续的请求应仍使用原始的URI。
* 与302相反当重新发出原始请求时不允许更改请求方法。 例如应该使用另一个POST请求来重复POST请求
* <p>
* 308 Permanent Redirect (永久重定向):
* <p>
* 请求和所有将来的请求应该使用另一个URI重复。
* 307和308重复302和301的行为但不允许HTTP方法更改。 例如,将表单提交给永久重定向的资源可能会顺利进行。
* <p>
* https://www.cnblogs.com/wuguanglin/p/redirect.html
*
* @return
*/
@Bean
RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions.route(RequestPredicates.GET(restPath + "/**"), (serverRequest) -> {
String url = serverRequest.path();
int index = url.indexOf(restPath);
// 取/rest的后面部分
String path = url.substring(index + restPath.length());
String query = ParamNames.API_NAME + "=" + path + "&" + ParamNames.VERSION_NAME + "=";
return ServerResponse
.temporaryRedirect(URI.create("/?" + query))
.build();
});
}
}