簡體   English   中英

使用 Jersey 客戶端發送多部分/混合請求

[英]Sending multipart/mixed request with Jersey Client

I want to dialogue with this API service programmatically using Jersey ( https://eclipse-ee4j.github.io/jersey/ )

這是 Spring 中的 Rest Controller 實現:

@PostMapping(
      value = "/api/my-endpoint",
      consumes = MediaType.MULTIPART_MIXED_VALUE)
  public void enrichInvoice(@RequestPart("metadata") Map<String, Object> request,
      @RequestPart("human") MultipartFile humanFile) {
    log.info(String.format("received request:%n%s", request));
  }

我的客戶實現是這樣的

...
     final Client client = ClientBuilder.newClient(new ClientConfig()
          .register(MultiPartFeature.class)
          .register(JacksonFeature.class)
      );

      final FileDataBodyPart filePart = new FileDataBodyPart("human",myFile()));

      final BodyPart metadata = new BodyPart().entity(voBuilder.generateMetadata());

      final MultiPart multiPartEntity = new MultiPart();
      multiPartEntity.bodyPart(metadata, MediaType.APPLICATION_JSON_TYPE);
      multiPartEntity.bodyPart(filePart);

      final WebTarget target = client
          .target("http://localhost:8080/api/my-endpoint");
      final Entity<MultiPart> entity = Entity
          .entity(multiPartEntity, multiPartEntity.getMediaType());
      log.info(entity.toString());
      final Response response = target
          .request()
          .post(entity);
      log.info(String.format("%s", response.readEntity(String.class)));
      response.close();
...

但我不斷收到此錯誤:

Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'metadata' is not present]

這是因為元數據部分必須命名為“元數據”。 而且我找不到使用 BodyPart 命名它的方法。 我還嘗試使用 FormDataBodyPart 構建元數據

FormDataBodyPart metadataBodyPart = new FormDataBodyPart("metadata", metadata,
          MediaType.APPLICATION_JSON_TYPE);

但結果相同。

你能幫我弄清楚我在 bodyPart 定義中遺漏了什么嗎?

謝謝


編輯:這是從我的客戶端實現發送的 http 請求

Content-Type: multipart/mixed;boundary=Boundary_1_1972899462_1597045386454
User-Agent: Jersey/2.29 (HttpUrlConnection 11.0.8)
MIME-Version: 1.0
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 765

--Boundary_1_1972899462_1597045386454
Content-Type: application/json

{"value":"key"}
--Boundary_1_1972899462_1597045386454
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="file.zip"; modification-date="Wed, 05 Aug 2020 16:52:52 GMT"; size=0; name="human"


--Boundary_1_1972899462_1597045386454--
]

解決方案,即使不是最佳的,也是將元數據視為文本文件

final Path tempFile = Files.createTempFile("prefix", "suffix");
File fileMetadata = Files.write(tempFile.toAbsolutePath(), JsonUtils.toString(metadata).getBytes());

final FileDataBodyPart metadataBodyPart = new FileDataBodyPart(
          "metadata",
          fileMetadata,
          MediaType.APPLICATION_JSON_TYPE);

final FileDataBodyPart human = new FileDataBodyPart("human", new File(humanReadableFile.getFileKey()));

try (final MultiPart multiPartEntity = new MultiPart()) {
        multiPartEntity.bodyPart(metadataBodyPart);
        multiPartEntity.bodyPart(human);

    final Response response = client
        .target("http://localhost:8080/api/my-endpoint")
        .request()
        .post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));
    log.debug(String.valueOf(response.getStatus()));
    log.debug(response.readEntity(String.class));
}

通過這種方式,請求正文必須按照 controller 實現的要求,包含名為“元數據”和“人類”的部分,並且仍然保持多部分/混合內容類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM