Commit e2b9ed5e authored by 赵啸非's avatar 赵啸非

修改配置文件

parent 4eb7d30e
package com.mortals.xhx.busiz.web; package com.mortals.xhx.busiz.web;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.xhx.busiz.rsp.ApiResp; import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.common.code.ApiRespCodeEnum; import com.mortals.xhx.common.code.ApiRespCodeEnum;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriBuilder;
import org.springframework.web.util.UriUtils;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.net.URI; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
@RestController @RestController
...@@ -24,11 +22,13 @@ public class ProxyController { ...@@ -24,11 +22,13 @@ public class ProxyController {
private final WebClient webClient; private final WebClient webClient;
private final OkHttpClient client = new OkHttpClient();
public ProxyController(WebClient.Builder webClientBuilder) { public ProxyController(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.build(); this.webClient = webClientBuilder.build();
} }
@PostMapping("/post") /* @PostMapping("/post")
@UnAuth @UnAuth
public Mono<ResponseEntity<String>> proxyPost( public Mono<ResponseEntity<String>> proxyPost(
@RequestParam Map<String, String> params, // 透传 URL 参数 @RequestParam Map<String, String> params, // 透传 URL 参数
...@@ -69,7 +69,7 @@ public class ProxyController { ...@@ -69,7 +69,7 @@ public class ProxyController {
return uriBuilder.build(); return uriBuilder.build();
}) })
/* .uri(uriBuilder -> { *//* .uri(uriBuilder -> {
uriBuilder.scheme("http"); uriBuilder.scheme("http");
uriBuilder.path(targetUrl); uriBuilder.path(targetUrl);
...@@ -78,7 +78,7 @@ public class ProxyController { ...@@ -78,7 +78,7 @@ public class ProxyController {
); );
log.info("proxyPost uriBuilder: {}", uriBuilder.build()); log.info("proxyPost uriBuilder: {}", uriBuilder.build());
return uriBuilder.build(); return uriBuilder.build();
})*/ })*//*
.headers(httpHeaders -> headers.forEach(httpHeaders::set)) .headers(httpHeaders -> headers.forEach(httpHeaders::set))
.bodyValue(body != null ? body : "") // 透传 Body .bodyValue(body != null ? body : "") // 透传 Body
.retrieve() .retrieve()
...@@ -89,6 +89,79 @@ public class ProxyController { ...@@ -89,6 +89,79 @@ public class ProxyController {
//不支持 //不支持
} }
return Mono.just(ResponseEntity.ok().body(JSON.toJSONString(rsp))); return Mono.just(ResponseEntity.ok().body(JSON.toJSONString(rsp)));
}*/
@PostMapping("/post")
public ResponseEntity<String> proxyPost(
@RequestParam Map<String, String> params, // 透传 Query Params
@RequestBody(required = false) String body, // 透传 Body
@RequestHeader Map<String, String> headers // 透传 Headers
) {
log.info("proxyPost params: {}, body: {}, headers: {}", params, body, headers);
ApiResp<String> rsp = new ApiResp<>();
String path = params.getOrDefault("path", "");
if (ObjectUtils.isEmpty(path)) {
rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
rsp.setMsg("path is empty!");
// return Mono.just(ResponseEntity.ok().body(JSON.toJSONString(rsp)));
}
String method = params.getOrDefault("method", "post");
params.remove("method");
params.remove("path");
String targetUrl = "http://127.0.0.1:11078/basics_api"+path; // 目标 URL
// 拼接 Query 参数
HttpUrl.Builder urlBuilder = HttpUrl.parse(targetUrl).newBuilder();
params.forEach(urlBuilder::addQueryParameter);
HttpUrl url = urlBuilder.build();
// 构造请求体
okhttp3.RequestBody requestBody = body != null ?
okhttp3.RequestBody.create(MediaType.parse("application/json"), body) :
okhttp3.RequestBody.create(null, new byte[0]);
if ("post".equalsIgnoreCase(method)) {
// 构造请求
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.post(requestBody);
// 透传 Headers(过滤掉 Host 避免冲突)
headers.forEach((key, value) -> {
if (!key.equalsIgnoreCase("host")) {
requestBuilder.addHeader(key, value);
}
});
// 发送请求
try (Response response = client.newCall(requestBuilder.build()).execute()) {
if (!response.isSuccessful()) {
return ResponseEntity.status(response.code()).body(response.message());
}
return ResponseEntity.status(response.code()).body(response.body().string());
} catch (IOException e) {
return null;
//return ResponseEntity.().body("Proxy error: " + e.getMessage());
}
} else if ("get".equalsIgnoreCase(method)) {
} else {
}
return null;
} }
......
...@@ -70,7 +70,7 @@ Content-Type: application/json ...@@ -70,7 +70,7 @@ Content-Type: application/json
### ###
POST http://192.168.0.250:11072/basic_api/entservice/ent/life/cycle/interlist POST http://192.168.0.250:11089/entservice/ent/life/cycle/interlist
Content-Type: application/json Content-Type: application/json
{"page": 1, "size": 10} {"page": 1, "size": 10}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment