Apache骆驼在传递Xml作为有效载荷并尝试将其封送给json时出错

时间:2018-10-18 14:08:17

标签: java xml apache-camel

我正在使用POSTMan来测试我的API,我试图通过POSTMAN将xml(类型:application / xml)传递给我的其余api端点。我想阅读此xml,然后将相应的信息存储在数据库中。

我试图首先通过骆驼将xml转换为json,然后读取数据并将其插入。我已经正确定义了路由(我认为是这样),但是我总是遇到以下错误:

"{ \"status\": 400, \"message\": \"an error occurred: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.ByteArrayInputStream@18eaa21b; line: 1, column: 2]\" }

我要发送的xml是:

    <ns1:ContractLinkEvent xmlns:ns1="some url" xmlns:ns0="some url"><Header xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Action>UPDATE</Action>
        <SubAction2 xsi:nil="true" />
        <SourceSystemUser xsi:nil="true" />
      </Header><ContractLink xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <SourceId>c4eb36da-4db7-e811-80ed-0050568e7b74</SourceId>
        <ContractLinkId>1509148</ContractLinkId>
        <PricingAgreement>
          <SourceId>86078759-49b7-e811-80ed-0050568e7b74</SourceId>
          <PricingAgreementId>1509146</PricingAgreementId>
          <ContractCode>C000012151</ContractCode>
          <Currency>USD</Currency>
          <ReferenceNumber>REF.012002</ReferenceNumber>
          <CreateDate>2018-09-13T11:37:10Z</CreateDate>
          <CreateUser>username</CreateUser>
          <CreateSystem>CRM</CreateSystem>
          <ModifiedDate>2018-09-13T12:02:42Z</ModifiedDate>
          <ModifiedUser>username</ModifiedUser>
          <ModifiedSystem>CRM</ModifiedSystem>
        </PricingAgreement>
        <BillingProfile>
          <SourceId>409cfdd9-781b-e511-b242-0050568e4f04</SourceId>
          <BillingProfileId>173886</BillingProfileId>
          <BillingProfileCode xsi:nil="true" />
          <BillingProfileName xsi:nil="true" />
          <DisplayName xsi:nil="true" />
          <BillingProfileTypeCode xsi:nil="true" />
          <SellingLE xsi:nil="true" />
          <APCode xsi:nil="true" />
          <PrimaryBillingFlag xsi:nil="true" />
          <BillToCreditCardFlag xsi:nil="true" />
          <PrintPriorityCode xsi:nil="true" />
          <InvoiceTypeCode xsi:nil="true" />
          <ElectronicInvoiceCode xsi:nil="true" />
          <PaperInvoiceCode xsi:nil="true" />
          <InvoiceFormat xsi:nil="true" />
          <ItemizedInvoiceFlag xsi:nil="true" />
          <DeliveryMethodCode xsi:nil="true" />
          <InvoiceEmailAddress xsi:nil="true" />
          <InvoiceEmailContactName xsi:nil="true" />
          <CompressionFlag xsi:nil="true" />
          <CompressionPassword xsi:nil="true" />
          <PaperInvoiceFeeFlag xsi:nil="true" />
          <BusinessCode xsi:nil="true" />
          <VatNumber xsi:nil="true" />
          <MinimumBillingFlag xsi:nil="true" />
          <VesselSortFlag xsi:nil="true" />
          <AllowReguideFlag xsi:nil="true" />
          <AllowRerateFlag xsi:nil="true" />
          <CreateUser xsi:nil="true" />
          <CreateSystem xsi:nil="true" />
          <ModifiedUser xsi:nil="true" />
          <ModifiedSystem xsi:nil="true" />
          <SalesOrg xsi:nil="true" />
          <DistChannel xsi:nil="true" />
          <Division xsi:nil="true" />
          <SalesDistrictCode xsi:nil="true" />
          <SalesOffice xsi:nil="true" />
          <Currency xsi:nil="true" />
          <PaymentTerms xsi:nil="true" />
          <DPId xsi:nil="true" />
          <BillingProfileGroup xsi:nil="true" />
          <InvoiceEmailList xsi:nil="true" />
          <SV xsi:nil="true" />
          <SAP xsi:nil="true" />
          <ServiceProfiles xsi:nil="true" />
          <SAPBlock xsi:nil="true" />
          <ContractClasses xsi:nil="true" />
        </BillingProfile>
        <StartDate>2018-09-16T00:00:00</StartDate>
        <CreateDate>2018-09-13T12:09:28Z</CreateDate>
        <CreateUser>Pratiksha Gupta</CreateUser>
        <CreateSystem>CRM</CreateSystem>
        <ModifiedDate>2018-09-13T12:11:23Z</ModifiedDate>
        <ModifiedUser>sv-CRMAPProxy AP Proxy</ModifiedUser>
        <ModifiedSystem>CRM</ModifiedSystem>
      </ContractLink></ns1:ContractLinkEvent>

我已经在线验证了xml,并且xml中没有错误。我还在骆驼中为端点定义了路由:

rest("/readXml")
        .consumes(MediaType.APPLICATION_XML)
        .post()
        //.outType(String.class)
                .to("direct:readxml");

        from("direct:readxml").streamCaching()
                .process(readAndInsertXml)
                 .marshal().xmljson() 

                 .setHeader(Exchange.HTTP_METHOD, constant("POST")) 
                .setHeader(Exchange.CONTENT_TYPE, constant("application/xml"))
                .to("mock:result").end();

这是我的readandInsertXml处理器,目前为空。

@Component
public class ReadAndInsertXml implements Processor{

    @Override
    public void process(Exchange exchange) throws Exception {

        Logger logger = LoggerFactory.getLogger(this.getClass());
    /*  SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();*/
        try {
            logger.info("inside processor method of read xml");
            logger.info(exchange.getIn().getBody().toString());
/*          Document doc = (Document) exchange.getIn().getBody();
            logger.info("after storing in dom document");
            System.out.println(doc.getAttributes())*/;
            exchange.getOut().setBody("sample");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我已经在网上进行了多次搜索,但是找不到解决方案。谁能帮忙我在做什么错

0 个答案:

没有答案