gpt4 book ai didi

java - Spring Rest 模板创建多部分表单/数据客户端,像 postman 一样工作,抛出不可转换的异常

转载 作者:行者123 更新时间:2023-12-01 19:47:11 24 4
gpt4 key购买 nike

我想将带有 multipart/form-data header 请求的 postman 客户端转换为 Spring 模板客户端。

postmanClient

现在,我有一个运行良好的基本休息 Controller 。

@RestController
@RequestMapping("/api")
public class MainConroller {

private static final Logger log = LoggerFactory.getLogger(MainConroller.class);
@Autowired
private MainService mainService;

public MainConroller(MainService mainService) {
this.mainService = mainService;
}

@PostMapping("/mails/send")
public void send(
@RequestParam("usertoken") String usertoken,
@RequestParam("sendTo") String sendTo,
@RequestParam("subject") String subject,
@RequestParam("content") String content,
@RequestParam(required = false, name = "files") List<MultipartFile> multipartFiles) {
log.debug("{}, {}, {}, {}", usertoken, sendTo, subject, content);

mainService.processMessage(usertoken, sendTo, subject, content, multipartFiles);

}

}

但是,我需要创建一个休息客户端,因此我使用了休息模板,它现在看起来像这样:

ArrayList<HttpMessageConverter<?>> converters = new ArrayList<>(
Arrays.asList(new MappingJackson2HttpMessageConverter(), new ResourceHttpMessageConverter(), new FormHttpMessageConverter()));
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(converters);
}
File file = new File("*********");

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, Object> fileValueMap = new LinkedMultiValueMap<>();
fileValueMap.add(file.getName(), file);
fileValueMap.add(file.getName(), file);
fileValueMap.add(file.getName(), file);

HttpEntity<MultiValueMap<String, Object>> filesPart = new HttpEntity<>(fileValueMap, httpHeaders);

// String key values part

MultiValueMap<String, String> stringPart = new LinkedMultiValueMap<>();
stringPart.add("usertoken", "test");
stringPart.add("sendTo", "test");
stringPart.add("subject", "test");
stringPart.add("content", "test");

HttpEntity<MultiValueMap<String, String>> objectPart = new HttpEntity<>(stringPart, httpHeaders);


MultiValueMap<String, Object> multiPartRequest = new LinkedMultiValueMap<>();
multiPartRequest.add("ObjectPart", objectPart);
multiPartRequest.add("FilesPart", filesPart);


HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multiPartRequest, httpHeaders);

String serverUrl = "****";

restTemplate().postForEntity(serverUrl, requestEntity, String.class);

问题是当我尝试发送帖子请求时,它抛出

Exception in thread "main" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [multipart/form-data]

更新

客户端的解决方案非常简单,您只需在对象值中发送字符串值,这些值将通过泛型自动转换为字符串。

此外,文件不能像文件一样发送,而是必须创建 FileSystemResource,这是客户端的完整代码:

@Service
public class RestTemplatePost {

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

public void prepareMessage() throws Exception {


HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("usertoken", "test");
form.add("sendTo", "test");
form.add("subject", "test");
form.add("content", "test");
form.add("files", getTestFile());
form.add("files", getTestFile());

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, httpHeaders);

String serverUrl = "******";

restTemplate().postForEntity(serverUrl, requestEntity, String.class);


}

public static Resource getTestFile() throws IOException {
Path testFile = Paths.get("C****");
System.out.println("Creating and Uploading Test File: " + testFile);
Files.write(testFile, "Hello World !!, This is a test file.".getBytes());
return new FileSystemResource(testFile.toFile());
}

}

最佳答案

你把事情搞得太复杂了。您应该使用单个映射来保存表单值,而不是映射的映射。除此之外,Spring Boot 已经提供了一个 RestTemplate,因此您无需再次配置自己的模板。

File file = new File("*********");

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("files", file);
form.add("files", file);
form.add("files", file);

form.add("usertoken", "test");
form.add("sendTo", "test");
form.add("subject", "test");
form.add("content", "test");

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, httpHeaders);

String serverUrl = "****";

restTemplate().postForEntity(serverUrl, requestEntity, String.class);

RestTemplate(或者实际上是FormHttpMessageConverter)会将其转换为正确的请求。

关于java - Spring Rest 模板创建多部分表单/数据客户端,像 postman 一样工作,抛出不可转换的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52755378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com