引起:org.apache.camel.InvalidPayloadException:

时间:2018-05-16 17:29:26

标签: json csv http jackson apache-camel

我已经将HTTP4文件上传器组合在一起,将数据推送到HTTP服务器环境。有2个系统,1个用于CSV数据文件,1个用于JSON传送。将CSV发送到CSV系统工作正常。但是,将数据转换为JSON并发送到JSON系统失败,但出现以下异常。错误是直观的,但是,我真的不知道如何处理它。

JSON不使用多部分表单数据,应该特别是IO流吗?作为我的第一个HTTP服务,我失去了协议接受度。显然JSON系统不期望Multipart数据,我不确定Json http发送应该是什么?谢谢你的帮助!

Caused by: org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: org.apache.http.entity.mime.MultipartFormEntity 
Caused by: No type converter available to convert from type: org.apache.http.entity.mime.MultipartFormEntity to the required type: java.io.InputStream with value org.apache.http.entity.mime.MultipartFormEntity

这里是适用于CSV数据文件的上传器类,但不适用于Json?

            LOG.info("Uploading File for CustKey: " + custKey + " and Tenant: " + tenant);          

            StringBuilder authHeader = new StringBuilder("Bearer "); 
            authHeader.append(token);           
            LOG.info("Authorization: " + authHeader.toString());

            exchange.setProperty("CamelCharsetName", "UTF-8");  //"CamelCharsetName" Exchange.CHARSET_NAME
            exchange.getIn().setHeader("CamelHttpCharacterEncoding", "UTF-8");   //"CamelHttpCharacterEncoding" Exchange.HTTP_CHARACTER_ENCODING
            exchange.getIn().setHeader("CamelAcceptContentType", "application/json");  //"CamelAcceptContentType" Exchange.ACCEPT_CONTENT_TYPE
            exchange.getIn().setHeader("CamelHttpUri", uploadUrl);  //"CamelHttpUri" Exchange.HTTP_URI
            exchange.getIn().setHeader("CamelHttpMethod", "POST");  //"CamelHttpMethod" Exchange.HTTP_METHOD
            exchange.getIn().setHeader("x-ge-csvformat", "ODB");
            exchange.getIn().setHeader("Tenant", tenant);
//          exchange.getIn().setHeader("Content-Type", "multipart/form-data");  //"Content-Type" ; boundary=
            exchange.getIn().setHeader("Authorization", authHeader.toString());

            // Process the file in the exchange body
            File file = exchange.getIn().getBody(File.class);
            String fileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
            LOG.info("fileName: " + fileName);

            MultipartEntityBuilder entity = MultipartEntityBuilder.create();
            entity.addBinaryBody("file", file);
            entity.addTextBody("name", fileName);

            exchange.getIn().setBody(entity.build()); //*** use for CSV uploads

这是路线,路线的唯一区别是JsonMapper过程

     <route 
        id="core.predix.upload.route"
        autoStartup="false" >
        <from uri="{{uploadEntranceEndpoint}}" />
        <process ref="customerEntitesProcessor" /> <!--  sets up the message with the customer environment entities to upload data -->
        <process ref="customerTokenProcessor" />   <!--  sets up the message with the cusotmer's token -->
        <process ref="jsonMapper" />
        <to uri="{{jsonEndpoint}}" />
        <process ref="uploadProcessor" />          <!--  conditions the message with the HTTP header info per customer env -->
        <setHeader headerName="CamelHttpUri">
            <simple>${header.UPLOADURL}?throwExceptionOnFailure=false</simple>
        </setHeader>
        <setHeader headerName="CamelHttpMethod">
            <constant>POST</constant>
        </setHeader>
        <to uri="http4://apm-timeseries-query-svc-prod.app-api.aws-usw02-pr.predix.io:443/v2/time_series/upload?throwExceptionOnFailure=false" />
        <log message="After POSTING JSON: ${body}" loggingLevel="INFO"/>    
        <to uri="{{afteruploadLocation}}" />
<!--        <log message="JSON Route: ${body}" loggingLevel="INFO"/>    -->
<!--        <to uri="{{jsonEndpoint}}" /> -->
    </route>

1 个答案:

答案 0 :(得分:0)

显然发送json是一个IO流。多部分表单数据Content-type仅用于文件传输,系统期望Multipart表单数据为Content-Type。

让它工作我只是将数据作为IO Stream发送并将Content-Type更改为application / json。

代码更改为,发送Json。

            StringBuilder authHeader = new StringBuilder("Bearer "); 
            authHeader.append(token);           
            LOG.info("Authorization: " + authHeader.toString());

            exchange.setProperty("CamelCharsetName", "UTF-8");  //"CamelCharsetName" Exchange.CHARSET_NAME
            exchange.getIn().setHeader("CamelHttpCharacterEncoding", "UTF-8");   //"CamelHttpCharacterEncoding" Exchange.HTTP_CHARACTER_ENCODING
            exchange.getIn().setHeader("CamelAcceptContentType", "application/json");  //"CamelAcceptContentType" Exchange.ACCEPT_CONTENT_TYPE
            exchange.getIn().setHeader("CamelHttpUri", uploadUrl);  //"CamelHttpUri" Exchange.HTTP_URI
            exchange.getIn().setHeader("CamelHttpMethod", "POST");  //"CamelHttpMethod" Exchange.HTTP_METHOD
            exchange.getIn().setHeader("x-ge-csvformat", "ODB");
            exchange.getIn().setHeader("Tenant", tenant);
            exchange.getIn().setHeader("Content-Type", "application/json");
            exchange.getIn().setHeader("Authorization", authHeader.toString());

            //*** use for CSV uploads - uncomment all commented lines for CSV file upload
            // Process the file in the exchange body
//          File file = exchange.getIn().getBody(File.class);
//          String fileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
//          LOG.info("fileName: " + fileName);

//          MultipartEntityBuilder entity = MultipartEntityBuilder.create();
//          entity.addBinaryBody("file", file);
//          entity.addTextBody("name", fileName);

//          exchange.getIn().setBody(entity.build());