我收到错误消息“ org.springframework.web.client.HttpClientErrorException $ BadRequest:400错误请求”

时间:2019-08-10 14:08:23

标签: java spring-boot post

在尝试在春季启动时从另一个GET API内部调用POST API时,出现此错误“ org.springframework.web.client.HttpClientErrorException $ BadRequest:400错误请求”。以下是代码。我也在get api中获取多部分文件作为输入,并希望将该文件发送到此内部post api

路径tempFile = Files.createTempFile(null,null);

            Files.write(tempFile, newFile.getBytes());
            File fileToSend = tempFile.toFile();

            FileSystemResource fr = new FileSystemResource(fileToSend);

            String baseUrl = "someURL which is correct";
            URI uri = new URI(baseUrl);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            //headers.set("Content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
            headers.set("Ocp-Apim-Subscription-Key","{key which i dont want to display}");

            MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
            body.add("newFile", fr);

            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> response = restTemplate.postForEntity(uri, requestEntity, String.class);

            return response;

1 个答案:

答案 0 :(得分:1)

在restTemplate.postForEntity方法中传递baseUrl字符串而不是uri并尝试