AdminC
AnNong。
采纳率50%
2019-08-07 16:25

restTemplate能否传递list集合?

已采纳

如下,我想要直接传递list过去 然后在客户端处理list

 RestTemplate restTemplate = new RestTemplate();
            for (ScanFamily scanFamily : scanFamilyList) {
                String requestWebUrl = url;
                //获取档案列表
               List<ArchiveFile> archiveFileList =  this.archiveFileManager.searchList(scanFamily.getFamilyNo());
                //TODO  直接传递list
                MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
                paramMap.add("archiveFileList",archiveFileList);
                String s = restTemplate.postForObject(requestWebUrl, paramMap, String.class);

但是它会一直报错:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.ArrayList]
    at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:292)
    at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:252)
    at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:242)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:194)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:1)
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:588)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:436)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:279)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • tiankongzhichenglyf tkzc_shark 2年前
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        String json = SerializeUtil.Serialize(archiveFileList);
    
        HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
    
        String result = restTemplate.postForObject(url, formEntity, String.class);
    
    点赞 评论 复制链接分享