Spring MVC 如何执行ProxyExchange发布多部分请求?

3z6pesqy  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(157)

我需要帮助!
我尝试使用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());

        });
    }

字符串

ttvkxqim

ttvkxqim1#

我找到了一个解决方案,即使我不知道它是否是最好的解决方案。

@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;
    }

字符串

相关问题