camel中嵌入javascript解析JSON数据问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

camel中嵌入javascript解析JSON数据问题

忠门精锐 2018-09-03 15:45:20 1492

源码:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="_camelContext1" xmlns="http://camel.apache.org/schema/spring">
    <route id="_fortest1">
        <from id="_from1" uri="file:D:\Tmp\File\jsonfiles\test?noop=true"/>
        <log id="_log1" message="file name is:${file:name},file contents is:${body}"/>
        <convertBodyTo id="_convertBodyTo1-1" type="java.lang.String"/>
        <setHeader headerName="js-msg" id="_setHeader1-2-2">
            <javaScript>
                   var obj = JSON.parse(request.getBody());
                   var msgObj = obj.msg;
                   JSON.stringify(msgObj);
               </javaScript>
        </setHeader>
        <log id="_log2" message="json contents name is:${header.js-name}, msg is:${header.js-msg}"/>
        <transform id="_transform1">
            <simple>${body}</simple>
        </transform>
    </route>
</camelContext>


运行环境:jboss-fuse-6.3.0.redhat-187
运行日志:
15:42:57,599 | INFO | xtenderThread-30 | OsgiSpringCamelContext | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | Apache Camel 2.17.0.redhat-630187 (CamelContext: _camelContext1) started in 0.029 seconds
15:42:57,600 | INFO | xtenderThread-30 | OsgiBundleXmlApplicationContext | 205 - org.apache.servicemix.bundles.spring-context - 3.2.16.RELEASE_1 | Publishing application context as OSGi service with properties {org.springframework.context.service.name=CamelTestBrg, Bundle-SymbolicName=CamelTestBrg, Bundle-Version=1.0.0}
15:42:57,602 | INFO | xtenderThread-30 | ContextLoaderListener | 210 - org.springframework.osgi.extender - 1.2.1 | Application context successfully refreshed (OsgiBundleXmlApplicationContext(bundle=CamelTestBrg, config=osgibundle:/META-INF/spring/*.xml))
15:42:58,603 | INFO | jsonfiles%5Ctest | _fortest1 | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | file name is:testMessage1.xml,file contents is:{"id":"4","name":"张三","msg":{"age":"12","sex":"男"}}
15:42:58,608 | ERROR | jsonfiles%5Ctest | DefaultErrorHandler | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | Failed delivery for (MessageId: ID-CHENJR-63601-1535955105805-14-1 on ExchangeId: ID-CHENJR-63601-1535955105805-14-2). Exhausted after delivery attempt: 1 caught: org.apache.camel.builder.script.ScriptEvaluationException: Failed to evaluate: js: var obj = JSON.parse(request.getBody());n | var msgObj = obj.msg;n | JSON.stringify(msgObj);. Cause: org.mozilla.javascript.EcmaError: ReferenceError: "JSON" is not defined. (#1)n | n | Message Historyn | ---------------------------------------------------------------------------------------------------------------------------------------n | RouteId ProcessorId Processor Elapsed (ms)n | [_fortest1 ] [_fortest1 ] [file://D:%5CTmp%5CFile%5Cjsonfiles%5Ctest?noop=true ] [ 6]n | [_fortest1 ] [_log1 ] [log ] [ 1]n | [_fortest1 ] [_convertBodyTo1-1 ] [convertBodyTo[java.lang.String] ] [ 0]n | [_fortest1 ] [_setHeader1-2-2 ] [setHeader[js-msg] ] [ 4]n | n | Stacktracen | ---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.builder.script.ScriptEvaluationException: Failed to evaluate: js: var obj = JSON.parse(request.getBody());
                   var msgObj = obj.msg;
                   JSON.stringify(msgObj);. Cause: org.mozilla.javascript.EcmaError: ReferenceError: "JSON" is not defined. (<Unknown source>#1)
at org.apache.camel.builder.script.ScriptBuilder.createScriptEvaluationException(ScriptBuilder.java:547)[266:org.apache.camel.camel-script:2.17.0.redhat-630187]
at org.apache.camel.builder.script.ScriptBuilder.evaluateScript(ScriptBuilder.java:416)[266:org.apache.camel.camel-script:2.17.0.redhat-630187]
at org.apache.camel.builder.script.ScriptBuilder.evaluate(ScriptBuilder.java:160)[266:org.apache.camel.camel-script:2.17.0.redhat-630187]

请大神们帮忙分析分析,应如何解决该问题?

机器学习/深度学习 XML JSON JavaScript 前端开发 Java Apache 数据格式 Spring
分享到
取消 提交回答
全部回答(1)
  • 游客bgx5ifdnbokuq
    2019-07-17 23:03:22

    JAVA 的 js 引擎中没有引入浏览器中原生的JSON 库,您可以找一个json库,在java脚本引擎中添加.


    已经回到

    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题