Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
E
enterprise-service-platform
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵啸非
enterprise-service-platform
Commits
e2b9ed5e
Commit
e2b9ed5e
authored
Mar 19, 2025
by
赵啸非
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改配置文件
parent
4eb7d30e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
9 deletions
+82
-9
enterprise-service-manager/src/main/java/com/mortals/xhx/busiz/web/ProxyController.java
.../main/java/com/mortals/xhx/busiz/web/ProxyController.java
+81
-8
enterprise-service-manager/src/test/java/com/mortals/httpclient/system.http
...-manager/src/test/java/com/mortals/httpclient/system.http
+1
-1
No files found.
enterprise-service-manager/src/main/java/com/mortals/xhx/busiz/web/ProxyController.java
View file @
e2b9ed5e
package
com.mortals.xhx.busiz.web
;
import
com.alibaba.fastjson.JSON
;
import
com.mortals.framework.annotation.UnAuth
;
import
com.mortals.xhx.busiz.rsp.ApiResp
;
import
com.mortals.xhx.common.code.ApiRespCodeEnum
;
import
lombok.extern.slf4j.Slf4j
;
import
okhttp3.*
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.util.ObjectUtils
;
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.util.UriBuilder
;
import
org.springframework.web.util.UriUtils
;
import
reactor.core.publisher.Mono
;
import
java.net.URI
;
import
java.nio.charset.StandardCharsets
;
import
java.io.IOException
;
import
java.util.Map
;
@RestController
...
...
@@ -24,11 +22,13 @@ public class ProxyController {
private
final
WebClient
webClient
;
private
final
OkHttpClient
client
=
new
OkHttpClient
();
public
ProxyController
(
WebClient
.
Builder
webClientBuilder
)
{
this
.
webClient
=
webClientBuilder
.
build
();
}
@PostMapping
(
"/post"
)
/*
@PostMapping("/post")
@UnAuth
public Mono<ResponseEntity<String>> proxyPost(
@RequestParam Map<String, String> params, // 透传 URL 参数
...
...
@@ -69,7 +69,7 @@ public class ProxyController {
return uriBuilder.build();
})
/* .uri(uriBuilder -> {
*/
/* .uri(uriBuilder -> {
uriBuilder.scheme("http");
uriBuilder.path(targetUrl);
...
...
@@ -78,7 +78,7 @@ public class ProxyController {
);
log.info("proxyPost uriBuilder: {}", uriBuilder.build());
return uriBuilder.build();
})*/
})*/
/*
.headers(httpHeaders -> headers.forEach(httpHeaders::set))
.bodyValue(body != null ? body : "") // 透传 Body
.retrieve()
...
...
@@ -89,6 +89,79 @@ public class ProxyController {
//不支持
}
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
;
}
...
...
enterprise-service-manager/src/test/java/com/mortals/httpclient/system.http
View file @
e2b9ed5e
...
...
@@ -70,7 +70,7 @@ Content-Type: application/json
###
POST http://192.168.0.250:110
72/basic_api
/entservice/ent/life/cycle/interlist
POST http://192.168.0.250:110
89
/entservice/ent/life/cycle/interlist
Content-Type: application/json
{"page": 1, "size": 10}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment