java - 在 Java 对象中发送 Multipart 和 JSON 数据 - Spring Boot Rest
问题描述
我有一个包含多部分数据和字符串数据的 java bean。我试图将它传递给一个休息客户端调用,该调用接受这个 java bean 输入并处理它。
下面是我的模型类、控制器和休息客户端。从我的休息客户打来电话时,我收到了这个异常。
Exception in thread "main" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.techidiocy.models.NHPdfMergeRequest] and content type [multipart/form-data]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:810)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:594)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:384)
模型类
import org.springframework.web.multipart.MultipartFile;
public class Candidate {
private String firstName;
private String lastName;
private MultipartFile resume;
//getters and setters
}
控制器类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class CandidateController {
@Autowired
private CandidateService candidateService;
@RequestMapping(method=RequestMethod.POST, path="/add")
public void add(@RequestBody Candidate request) {
// do some processing
String firstName = request.getFirstName();
String lastName = request.getLastName();
MultipartFile resume = request.getResume();
candidateService.add(firstName, lastName, resume);
}
}
休息客户端
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.client.RestTemplate;
public class CandidateClient {
public static void main(String[] args) throws IOException {
String serverURL = "http://localhost:8080/add";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
Candidate candidate = new Candidate();
candidate.setFirstName("John");
candidate.setLastName("Doe");
candidate.setResume(new MockMultipartFile("tmp.pdf", FileUtils.readFileToByteArray(new File("/home/john/resume/john.pdf"))));
HttpEntity<Candidate> httpEntity = new HttpEntity<Candidate>(candidate, headers);
RestTemplate client = new RestTemplate();
client.postForEntity(serverURL, httpEntity, Resource.class);
}
}
注意:我还尝试在 rest 客户端中将标头内容类型设置为 json,然后在控制器中将所有值设置为 Null。headers.setContentType(MediaType.APPLICATION_JSON);
我也在互联网上搜索过这种情况,但我无法找到解决方案。
我还尝试单独传递所有参数(不是作为 java bean 的一部分),然后我就可以让它工作。
解决方案
推荐阅读
- c# - 访问 Visual Studio 的 $exception 变量
- machine-learning - 文本序列匹配的深度学习模型
- wordpress - 更改分页链接
- .net-core - 从 .net-core-2.2 升级到 .net-core-3.1 后,我发布的应用程序的输出目录中的 dotnet.exe 是什么?
- java - 给定长度为 k 的字符串,如何生成长度为 n 的所有排列
- r - R - 按索引从每一行中选择元素
- json - 将 JSON 格式的文件读入 pandas DataFrame
- node.js - 使用前缀选项时,Google Cloud Storage API 不返回文件
- java - E/RecyclerView:没有附加适配器;跳过片段中的布局错误
- reactjs - Jest 在尝试使用酶测试 React 组件时遇到了意外令牌