我在互联网上看到了许多有关从 Spring-Boot Rest Template 发送 multi-part 请求的示例,但实际上对我没有任何帮助,但我总是最终得到一个400错误的请求。我不确定错误是否与我用来存储文件详细信息的FileSystemResource
类有关。
这是我的控制器类。
@RequestMapping(method = RequestMethod.POST, consumes = { "multipart/form-data" },value = "/submit")
public ResponseEntity securedAcceptJob(
@RequestParam(value = "files" , required = false)MultipartFile multipartFile,
@RequestParam(value = "name" , required = true) String name
) throws NotInitializedException,CouldNotSendException {}
public void submitResponse(SrResultEntity srResultEntity){
String path = "des/submit";
LinkedMultiValueMap<String,Object> requestEntity = new LinkedMultiValueMap<>();
requestEntity.add("name",srResultEntity.getTitle());
requestEntity.add("files",getUserFileResource());
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON_UTF8_VALUE);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String,Object>> httpEntity = new HttpEntity(requestEntity, headers);
URI uri = UriComponentsBuilder.fromUriString(buildPath(path)).buildAndExpand(pathVariables).toUri();
final ResponseEntity<String> stringResponseEntity =
restClientHelper.getRestTemplate().exchange(uri,HttpMethod.POST, httpEntity, String.class);
}
public static Resource getUserFileResource() throws IOException {
//todo replace tempFile with a real file
Path tempFile = Files.createTempFile("upload-test-file", ".txt");
Files.write(tempFile, "some test content...\nline1\nline2".getBytes());
System.out.println("uploading: " + tempFile);
File file = tempFile.toFile();
//to upload in-memory bytes use ByteArrayResource instead
return new FileSystemResource(file);
}
我无法弄清楚我应该在代码中更改什么,这是我调试应用程序时的输出。
<{名称= [dwad],消息= [空],电子邮件= [m@yhaoo.com],文件= [文件[/tmp/upload-test-file5318593104290391569.txt]]},{ Accept = [application / json; charset = UTF-8],X-Auth-Token = [6ce45ed4-3278-4531-98fa-fe5949000380],Content-Type = [multipart / form-data]}> < / p>
即使我没有在请求中发送文件,错误仍然存在。
堆栈跟踪:
org.springframework.web.client.HttpClientErrorException:400错误 要求 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) 〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) 〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]在 org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) 〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]在 org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766) 〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724) 〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:698) 〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]在 org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:619) 〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]在 com.itesco.sam2.restclient.RestServiceClient.multiPartPost(RestServiceClient.java:52) 〜[classes /:na]在 com.itesco.sam2.restclient.des.DesServiceClient.submitResponse(DesServiceClient.java:131) 〜[classes /:na]在 com.itesco.sam2.ui.views.des.FileListView.sendReplies(FileListView.java:149) 〜[classes /:na]在 com.itesco.sam2.ui.views.AbstractEditorDialog.saveClicked(AbstractEditorDialog.java:202) 〜[classes /:na]在 com.itesco.sam2.ui.views.AbstractEditorDialog.lambda $ open $ cc13c671 $ 1(AbstractEditorDialog.java:191) 〜[classes /:na]在 com.vaadin.flow.component.ComponentEventBus.fireEvent(ComponentEventBus.java:132) 〜[流服务器-1.0.0.jar:na]在 com.vaadin.flow.component.ComponentEventBus.handleDomEvent(ComponentEventBus.java:326) 〜[流服务器-1.0.0.jar:na]在 com.vaadin.flow.component.ComponentEventBus.lambda $ addDomTrigger $ 5ee67f2b $ 1(ComponentEventBus.java:190) 〜[流服务器-1.0.0.jar:na]在 com.vaadin.flow.internal.nodefeature.ElementListenerMap.lambda $ fireEvent $ 2(ElementListenerMap.java:378) 〜[流服务器-1.0.0.jar:na]在 java.util.ArrayList.forEach(ArrayList.java:1257)〜[na:1.8.0_171]在 com.vaadin.flow.internal.nodefeature.ElementListenerMap.fireEvent(ElementListenerMap.java:378) 〜[流服务器-1.0.0.jar:na]在 com.vaadin.flow.server.communication.rpc.EventRpcHandler.handleNode(EventRpcHandler.java:58) 〜[流服务器-1.0.0.jar:na]在 com.vaadin.flow.server.communication.rpc.AbstractRpcInvocationHandler.handle(AbstractRpcInvocationHandler.java:63) 〜[流服务器-1.0.0.jar:na]在 com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocationData(ServerRpcHandler.java:377) 〜[流服务器-1.0.0.jar:na]在 com.vaadin.flow.server.communication.ServerRpcHandler.lambda $ handleInvocations $ 0(ServerRpcHandler.java:367) 〜[流服务器-1.0.0.jar:na]在 java.util.ArrayList.forEach(ArrayList.java:1257)〜[na:1.8.0_171]