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

开发者社区> 问答> 正文

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

2018-09-03 15:45:20 2343 1

源码:

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]

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

取消 提交回答
全部回答(1)
  • 游客bgx5ifdnbokuq
    2019-07-17 23:03:22

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


    已经回到

    0 0
相关问答

40

回答

[@徐雷frank][¥20]什么是JAVA的平台无关性

大河人家 2018-10-29 23:55:20 144734浏览量 回答数 40

162

回答

惊喜翻倍:免费ECS+免费环境配置~!(ECS免费体验6个月活动3月31日结束)

豆妹 2014-10-29 17:52:21 226173浏览量 回答数 162

145

回答

【新手入门】云服务器linux使用手册

fanyue88888 2012-11-26 17:14:18 157705浏览量 回答数 145

8

回答

OceanBase 使用动画(持续更新)

mq4096 2019-02-20 17:16:36 337022浏览量 回答数 8

110

回答

OSS存储服务-客户端工具

newegg11 2012-05-17 15:37:18 295573浏览量 回答数 110

22

回答

爬虫数据管理【问答合集】

我是管理员 2018-08-10 16:37:41 147249浏览量 回答数 22

18

回答

阿里云开放端口权限

xcxx 2016-07-20 15:03:33 646814浏览量 回答数 18

31

回答

[@倚贤][¥20]刚学完html/css/js的新手学习servlet、jsp需要注意哪些问题?

弗洛伊德6 2018-10-27 21:52:43 146048浏览量 回答数 31

249

回答

阿里云LNAMP(Linux + Nginx + Apache + MySQL + PHP)环境一键安装脚本

云代维 2014-02-14 15:26:06 305645浏览量 回答数 249

24

回答

【精品问答】python技术1000问(1)

问问小秘 2019-11-15 13:25:00 475707浏览量 回答数 24
+关注
0
文章
1
问答
问答排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载