无法使用Jar作为有效负载进行POST服务调用

时间:2018-06-06 13:05:54

标签: spring spring-boot resttemplate

我正在尝试使用JAR文件作为有效负载进行POST服务调用,我得到以下错误

  

找不到合适的HttpMessageConverter请求类型   [org.springframework.util.LinkedMultiValueMap]和内容类型   [应用/八位字节流]

我尝试的代码如下,这里的任何帮助都会很棒

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("*.jar").getFile());

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.AUTHORIZATION, authHeader);
    headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

    LinkedMultiValueMap<String, Object> payLoad = new LinkedMultiValueMap<String, Object>();
    payLoad.add("file", new FileSystemResource(file));

    HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(payLoad, headers);
    try {
        RestTemplate restTemplate = new RestTemplate();         

        ResponseEntity<Object> response = restTemplate.postForEntity(url, Object.class);
    } catch (RestClientException ex) {
        ex.printStackTrace();       
    }

1 个答案:

答案 0 :(得分:0)

听起来你必须添加一个HttpMessageConverter来处理八位字节流。尝试在WebConfig

中添加类似的内容
@Override
public void configureMessageConverters(
    List<HttpMessageConverter<?>> converters) {

    messageConverters.add(new ResourceHttpMessageConverter());

    super.configureMessageConverters(converters);
}