提问人:Khaled 提问时间:10/25/2023 更新时间:10/25/2023 访问量:57
如何执行 ProxyExchange 发布分段请求?
how perform ProxyExchange post multipart request?
问:
请我需要帮助!
我尝试使用 ProxyExchange 重定向多部分请求,但没有成功。
以前有人做过吗?
先谢谢你
@PostMapping("/service")
public ResponseEntity<byte[]> proxyPost(ProxyExchange<byte[]> proxy, @RequestHeader("authorization") String authorizationHeader,@RequestPart("file") MultipartFile file) throws Exception {
proxy.header("Authorization-getway",authorizationHeader);
return proxy.uri(dzOptServiceUri.toString() + "/api/service/").post(response -> {
return ResponseEntity.status(response.getStatusCode()) //
.headers(response.getHeaders()) //
.header("Authorization-getway", authorizationHeader) //
.body(response.getBody());
});
}
答:
0赞
Khaled
10/25/2023
#1
即使我不知道它是否是最好的解决方案,我也找到了解决方案
@PostMapping("/service")
public ResponseEntity<byte[]> proxyPost(ProxyExchange<byte[]> proxy,
@ModelAttribute ServiceDto serviceDto,
@RequestHeader("authorization") String authorizationHeader,
@RequestPart("file") MultipartFile file
) throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
proxy.headers(headers);
proxy.body(this.buildBody(file,serviceDto));
proxy.header("Authorization-getway",authorizationHeader);
return proxy.uri(dzOptServiceUri + "/api/service/").post(response ->
ResponseEntity.status(response.getStatusCode()) //
.headers(response.getHeaders()) //
.body(response.getBody())
);
}
private MultiValueMap<String, Object> buildBody(MultipartFile file,ServiceDto serviceDto) throws IOException {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("libelle", serviceDto.getLibelle());
body.add("definition", serviceDto.getDefinition());
body.add("path", serviceDto.getPath());
body.add("icone", serviceDto.getPath());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// Ajouter un fichier au corps de la requête
body.add("file", new org.springframework.core.io.ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return "";
}
});
return body;
}
评论