【发布时间】:2021-05-06 21:08:43
【问题描述】:
我有 2 个 Spring Boot 应用程序,一个作为前端运行,另一个作为后端服务运行。从前端我对后端服务进行 api 调用,我发送的两个参数都显示为 null。我认为问题出在其余模板中。
更新
所以我注意到如果我省略内容值,那么它会起作用。由于内容是大于 1mb 的文件的内容,我在 application.yml 中添加了以下内容:
spring.servlet.multipart.max-file-size: 10MB
spring.servlet.multipart.max-request-size: 10MB
这是我在本期发布的代码中更新的代码: How to POST form data with Spring RestTemplate?
但我仍然没有在后端控制器中得到值,而是两个值都是空的。
public void upload(byte[] content, String name) {
String encodedString = Base64.getEncoder().encodeToString(content);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("fileName", name);
map.add("content", encodedString);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity(backendUrl + "/upload", request, String.class);
log.debug("Response from upload: " + response);
}
这是后端的控制器。文件名和内容均为空:
@CrossOrigin
@SneakyThrows
@ResponseBody
@PostMapping(value = "/upload")
public ResponseEntity<String> upload(@ModelAttribute FormModel form) {
byte[] decodedBytes = Base64.getDecoder().decode(form.getContent());
uploadService.upload(decodedBytes, form.getFileName());
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Content-Type", "application/json");
return ResponseEntity.ok().headers(responseHeaders).body("Uploaded");
}
谁能看看这段代码有什么问题?
提前致谢。
【问题讨论】:
标签: java spring-boot resttemplate