简体   繁体   English

使用FreeMarker的带有Smooks的Java到XML(SOAP)

[英]Java to XML (SOAP) with Smooks using FreeMarker

Hey StackOverflow users, 嘿StackOverflow用户,

I'm currently working on a SOA Project. 我目前正在从事SOA项目。 As an Application Server I use JBoss 5.1 with JBoss ESB 4.11 deployed. 作为应用服务器,我将JBoss 5.1与已部署的JBoss ESB 4.11结合使用。

I try to implement a Web Service which recieves SOAP Messages from Clients and send responses as SOAP messages as well. 我尝试实现一个Web服务,该服务从客户端接收SOAP消息,并以SOAP消息的形式发送响应。
When a SOAP message request is recieved by this Web Service I'm using Smooks to transform this message into Java Objects so i can process the request. 当此Web服务收到SOAP消息请求时,我正在使用Smooks将此消息转换为Java对象,以便我可以处理该请求。
When im done processing, I want to transform Java Objects to XML (a SOAP reply), again with Smooks. 完成处理后,我想再次使用Smooks将Java对象转换为XML(SOAP答复)。

I'm stuck on the transformation from Java to XML. 我停留在从Java到XML的转换上。
My Action Chain in the jboss-esb.xml file looks like this: jboss-esb.xml文件中的“我的操作链”如下所示:

<services>
  <service name="myWS" description="A Service" category="WS">
        <listeners>
            <jms-listener name="myListener" busidref="myChannel"/>
        </listeners>
        <actions mep="RequestResponse" inXsd="in.xsd" outXsd="out.xsd">
            <!-- Transform incomming SOAP Message into JavaBean objects -->
            <action name="xml2java-transform" class="org.jboss.soa.esb.smooks.SmooksAction">
                <property name="smooksConfig" value="/smooks/smooks-config-soap2java.xml"/>
                <property name="resultType" value="JAVA" />
            </action>

            <action name="processRequest" class="example.soa.ProcessRequest" process="process">
                <property name="config" value="val"/>
            </action>

            <!-- Transform outgoing JavaBean objects into SOAP Message -->
            <action name="java2xml-transform" class="org.jboss.soa.esb.smooks.SmooksAction">
                <property name="smooksConfig" value="/smooks/smooks-config-java2soap.xml"/>
                <property name="reportPath" value="/smooks/report.html"/>
                <property name="resultType" value="STRING" />
            </action>
        </actions>
    </service>
</services>

My smooksConfig of the second SmooksAction looks like this: smooksConfig第二SmooksAction的是这样的:

<smooks-resource-list   xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                    xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
                    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">

<core:filterSettings type="SAX" defaultSerialization="false"/> 

<resource-config selector="example.JavaObjectMessage">
    <resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>


<ftl:freemarker applyOnElement="example.JavaObjectMessage">
    <!--<ftl:template>/freemarker/acknowledge.flt</ftl:template> -->
    <ftl:template>
        <!-- 
        <sg:Message xmlns:sg="urn:http://example">
            <sg:MessageType>${.vars[example.JavaObjectMessage].messageType}</sg:MessageType>
        </sg:Message >
        -->
    </ftl:template>
</ftl:freemarker>
</smooks-resource-list>

After the processRequest Action is done processing the incomming request it will attatch the example.JavaObjectMessage class to the esb message. processRequest Action处理完传入的请求之后,它将把example.JavaObjectMessage类附加到esb消息。 So the second SmooksAction will have access to this Object. 因此,第二个SmooksAction将有权访问此Object。

My question is: how can i access the attributes of the 'example.JavaObjectMessage' in the smooksConfig ? 我的问题是:如何在smooksConfig访问“ example.JavaObjectMessage”的smooksConfig And to what does the applyOnElement refer to in the flt:tamplate section? flt:tamplate部分中, applyOnElement指的是什么?

I already read the Smooks User Guide and in the JBoss Community I posted this question too. 我已经阅读了《 Smooks用户指南》,并且在JBoss社区中也发布了此问题。

I appreciate any help! 感谢您的帮助!

Regards 问候

I finally solved the problem! 我终于解决了问题! I had a hard time to understand the mechanism of how to transform Java to XML with the SmooksAction and im sure i still don't understand everything. 我很难理解如何使用SmooksActionJava转换为XML的机制,并且确定我仍然不了解所有内容。

Anyways what i did was: 无论如何,我所做的是:
1. took a closer look at the SAX filter within the smooks-config.xml file 1.仔细查看smooks-config.xml文件中的SAX筛选器
2. found out, that the SAX filter will create an XML document with the package name of the Java Object as root element and all its attributes as child elements. 2.发现, SAX筛选器将创建一个XML文档,其中Java Object的包名称为根元素,其所有属性为子元素。
3. the XML document created by the SAX filter can then be used to create a virtual object (in this case a HashMap ) 3.然后,可以使用SAX筛选器创建的XML文档来创建虚拟对象(在本例中为HashMap )。
4. the HashMap can then be used as the input for the FreeMarker template 4.然后,可以将HashMap用作FreeMarker模板的输入

So my smooks-config.xml file looks now like this: 所以我的smooks-config.xml文件现在看起来像这样:

<?xml version="1.0" encoding='UTF-8'?>
<smooks-resource-list   xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                    xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
                    xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd"
                    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">

<resource-config selector="global-parameters">
    <param name="stream.filter.type">SAX</param>
</resource-config>

<jb:bean beanId="message" class="java.util.HashMap" createOnElement="example.JavaObjectMessage">
    <jb:value property="messageType" decoder="String" data="example.JavaObjectMessage/messageType"/>
</jb:bean>

<ftl:freemarker applyOnElement="example.JavaObjectMessage">
    <ftl:template>
    <!--     
        <sg:Message xmlns:sg="urn:http://message">
            <sg:MessageType>${message.messageType}</sg:MessageType>
        </sg:Message>
    --> 
    </ftl:template>
</ftl:freemarker>
</smooks-resource-list>

as explained before: first use the SAX filter, second create a virtual object ( HashMap in this case), third: do the templating with free marker 如前所述:首先使用SAX过滤器,其次创建一个虚拟对象(在这种情况下为HashMap ),第三步:使用自由标记进行模板制作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM