我有获取ServerRequest然后将其转换为WebClient并将其代理到目标的组件。
请求的Content-Type为multipart/form-data

return getBody(request)
    .flatMap { body ->
      WebClient.builder()
          .baseUrl(path)
          .defaultCookies { x ->
            request.cookies()
                .forEach { (k, v) -> x[k] = v.map { it.value } }
          }
          .defaultHeaders { x ->
            request.headers().asHttpHeaders()
                .forEach { (k, v) -> x[k] = v }
          }
          .defaultUriVariables(request.queryParams())
          .build()
          .method(request.method() ?: HttpMethod.GET)
          .body(body)
          .exchange()
    }

  private fun getBody(request: ServerRequest): Mono<BodyInserter<*, in ClientHttpRequest>> {
    return request.multipartData()
        .filter { it.isNotEmpty() }
        .map<BodyInserter<*, in ClientHttpRequest>> { multipart ->
          BodyInserters.fromMultipartData(multipart)
        }
        .switchIfEmpty(Mono.fromCallable {
          val body = request.bodyToMono(String::class.java)
          BodyInserters.fromPublisher(body, String::class.java)
        })
  }


我对多部分请求有问题:尝试创建具有多部分的正文以将其发送到目标时,它失败。

我有以下配置:

@Configuration
open class SwitcherMultipartConfigurer : WebFluxConfigurer {

  override fun configureHttpMessageCodecs(configurer: ServerCodecConfigurer) {
    configurer.customCodecs().reader(
        MultipartHttpMessageReader(SynchronossPartHttpMessageReader())
    )
    configurer.customCodecs().writer(
        MultipartHttpMessageWriter(

        )
    )
  }
}


可以是什么?错误是:

 observed an error org.springframework.core.codec.CodecException: No suitable writer found for part: file
      | 2019-06-25T13:33:49.069006743Z  at org.springframework.http.codec.multipart.MultipartHttpMessageWriter.encodePart(MultipartHttpMessageWriter.java:300)
      | 2019-06-25T13:33:49.069013261Z  at org.springframework.http.codec.multipart.MultipartHttpMessageWriter.lambda$encodePartValues$4(MultipartHttpMessageWriter.java:253)
      | 2019-06-25T13:33:49.069018616Z  at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
      | 2019-06-25T13:33:49.069023613Z  at java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1235)
      | 2019-06-25T13:33:49.069028522Z  at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
      | 2019-06-25T13:33:49.069033432Z  at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
      | 2019-06-25T13:33:49.069038361Z  at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
      | 2019-06-25T13:33:49.069043368Z  at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
      | 2019-06-25T13:33:49.069048212Z  at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
      | 2019-06-25T13:33:49.069053089Z  at org.springframework.http.codec.multipart.MultipartHttpMessageWriter.encodePartValues(MultipartHttpMessageWriter.java:253)
      | 2019-06-25T13:33:49.069058115Z  at org.springframework.http.codec.multipart.MultipartHttpMessageWriter.lambda$writeMultipart$3(MultipartHttpMessageWriter.java:234)

最佳答案

找到了方法。我代理的多部分数据不是作为Part创建的MultipartHttpReader,而是作为ByteArray

  val builder = MultipartBodyBuilder()
  multipart
      .toSingleValueMap()
      .forEach {
        builder.asyncPart(it.key, it.value.content(), DataBuffer::class.java)
            .headers { httpHeaders -> it.value.headers().forEach { httpHeaders[it.key] = it.value } }
      }
  BodyInserters.fromMultipartData(builder.build())


需要标头来保存显示在Content-Disposition标头中的内容类型和文件名。

10-08 17:36