【问题标题】:org.apache.camel.InvalidPayloadException when sumbitting form data提交表单数据时出现org.apache.camel.InvalidPayloadException
【发布时间】:2021-11-04 21:23:56
【问题描述】:

我的 quarkus 代码中有以下 apache camel 路由:

rest("/")
        .get("/v*").to(MY_SERVICE)
        .post("/v*").to(MY_SERVICE)
        .put("/v*").to(MY_SERVICE)
        .delete("/v*").to(MY_SERVICE)

当我使用以下代码发送带有表单数据的 POST 请求时:

given().urlEncodingEnabled(true)
        .param("email_address", "user@example.com")
        .post("/v3/test");

我收到以下错误:

ERROR [org.apa.cam.pro.err.DefaultErrorHandler] (vert.x-worker-thread-1) Failed delivery for (MessageId: 2B577A1B8400C13-0000000000000001 on ExchangeId: 2B577A1B8400C13-0000000000000001). Exhausted after delivery attempt: 1 caught: org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: {email_address=user@example.com} of type: java.util.HashMap on: Message[2B577A1B8400C13-0000000000000001]. Caused by: No type converter available to convert from type: java.util.HashMap to the required type: java.io.InputStream with value {email_address=user@example.com}. Exchange[2B577A1B8400C13-0000000000000001]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: java.util.HashMap to the required type: java.io.InputStream with value {email_address=user@example.com}]

Message History (complete message history is disabled)
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route10           ] [route10           ] [from[platform-http:///v*?httpMethodRestrict=POST]                             ] [        12]
        ...
[route6            ] [toD3              ] [{{mailgun.url}}${headers.CamelHttpUri}?bridgeEndpoint=true&throwExceptionOnFai] [         0]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
: org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: {email_address=user@example.com} of type: java.util.HashMap on: Message[2B577A1B8400C13-0000000000000001]. Caused by: No type converter available to convert from type: java.util.HashMap to the required type: java.io.InputStream with value {email_address=user@example.com}. Exchange[2B577A1B8400C13-0000000000000001]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: java.util.HashMap to the required type: java.io.InputStream with value {email_address=user@example.com}]
        at org.apache.camel.support.MessageSupport.getMandatoryBody(MessageSupport.java:117)
        at org.apache.camel.component.http.HttpProducer.createRequestEntity(HttpProducer.java:576)
        at org.apache.camel.component.http.HttpProducer.createMethod(HttpProducer.java:477)
        at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:118)
        at org.apache.camel.support.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:66)
        at org.apache.camel.processor.SendDynamicProcessor.lambda$process$0(SendDynamicProcessor.java:195)
        at org.apache.camel.support.cache.DefaultProducerCache.doInAsyncProducer(DefaultProducerCache.java:317)
        at org.apache.camel.processor.SendDynamicProcessor.process(SendDynamicProcessor.java:180)
        at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:395)
        at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:148)
        at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:60)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:147)
        at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:312)
        at org.apache.camel.component.platform.http.vertx.VertxPlatformHttpConsumer.lambda$handleRequest$2(VertxPlatformHttpConsumer.java:184)
        at io.vertx.core.impl.ContextImpl.lambda$executeBlocking$2(ContextImpl.java:313)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: java.util.HashMap to the required type: java.io.InputStream with value {email_address=user@example.com}
        at org.apache.camel.impl.converter.CoreTypeConverterRegistry.mandatoryConvertTo(CoreTypeConverterRegistry.java:275)
        at org.apache.camel.support.MessageSupport.getMandatoryBody(MessageSupport.java:115)
        ... 18 more

出了什么问题?如何解决?

【问题讨论】:

    标签: apache-camel rest-assured


    【解决方案1】:

    尝试使用formParams方法发送表单数据:

    Map<String, String> formParams = new HashMap<>();
    formParams.put("email_address", "user@example.com");
    
    given()
      .contentType("application/form-data")
      .formParams(formParams)
      .post("/v3/test");
    

    【讨论】:

    • 是的,放心使用。请查看我的代码 sn-p,我在 post() 之前确实有 .param("email_address", "user@example.com")。
    猜你喜欢
    • 2015-09-16
    • 2018-10-22
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2014-02-25
    • 2018-11-24
    相关资源
    最近更新 更多