0

我正在从 UI 进行多部分表单数据发布调用以上传文件。从 UI,调用转到组件 A 控制器,您可以将其视为普通组件或总线,它只是读取流,通过 rest 调用将流传递到其他组件 B 控制器,该调用上传文件并在 json 中发送回有关上传文件的元数据格式。

问题:调用成功进行到组件 B 控制器,文件已上传,但是当组件 B 的响应到达组件 A 时,出现以下错误

“消息”:“无法提取响应:没有找到适合响应类型 [class java.lang.Object] 和内容类型 [multipart/form-data;boundary=HWhdmg6KNJw_kaP4wWnLyoAb9htc8StF4;charset=UTF-8] 的 HttpMessageConverter”

有人可以帮忙吗?

组件 A 代码

@PostMapping(value = "/v3/documents",consumes =  MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity uploadMultipartDocument(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)  {
    ResponseEntity<?> responseEntity = null;
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    headers.set(HznAppsHeaderConstant.DOCUMENT_TYPE, HznAppsUtil.getHeaderFromCustomHeaderParameterProvider(customHeaderParameterProvider, HznAppsHeaderConstant.DOCUMENT_TYPE));
    InputStream stream =null;
    MultiValueMap<String, Object> params= new LinkedMultiValueMap<String, Object>();
    try{
      ServletFileUpload upload = new ServletFileUpload();
      FileItemIterator iterStream = upload.getItemIterator(req);
      while (iterStream.hasNext()){
        FileItemStream item = iterStream.next();
        stream = item.openStream();
        if (!item.isFormField()  && item.getFieldName().equalsIgnoreCase("file")) {
          InputStreamResource inputStream = new InputStreamResource(stream) {
            @Override
            public String getFilename() {
              return item.getName();
            }

            @Override
            public long contentLength() {
              return -1;
            }
          };
          params.add("file", inputStream);
          break;
        }
      }
      final HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers);
      restTemplate.getInterceptors().clear();
      SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
      requestFactory.setBufferRequestBody(false);
      restTemplate.setRequestFactory(requestFactory);
      responseEntity = restTemplate.exchange("Component B URL", HttpMethod.POST, httpEntity, Object.class);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return responseEntity;
  }

B组份代码

@RequestMapping(value = "/api/v3/documents", produces = {"application/json"}, consumes = {"multipart/form-data"}, method = RequestMethod.POST)
    public ResponseEntity uploadMultipartDocument(HttpServletRequest request) {
        ResponseDocument responseDocument = null;
        try {
            responseDocument = documentsService.uploadMultiPartDocument(request,documentType); // Doing Soap call to upload document and its works fine in this method
        } catch (Exception e) {
            e.printStackTrace();
           
        }
        return new ResponseEntity<>(responseDocument, HttpStatus.OK);
    }

谢谢

4

0 回答 0