我想将这个带有 multipart/form-data 标头请求的邮递员客户端转换为 spring 模板客户端。
现在,我有一个基本的休息控制器,效果很好。
@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);
}
问题是当我尝试发送发布请求时,它抛出
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]
凤凰求蛊
慕标琳琳
相关分类