1

我正在使用 smooks api 进行 java-xml 和 xml-java 转换。此代码在 jboss4.2.2 中工作,但迁移 jboss7.1.1 时出现异常是“无法访问资源数据流”

示例代码:

private String runSmooksTransform(Object inputJavaObject)
        throws IOException, SAXException {
              Smooks smooks = new Smooks("/WEB-INF/smooks-config.xml");
            try {
        ExecutionContext executionContext = smooks.createExecutionContext();
        StringWriter writer = new StringWriter();

        smooks.filterSource(executionContext,
                new org.milyn.payload.JavaSource(inputJavaObject),
                new StreamResult(writer));

        return writer.toString();
        } finally {
        smooks.close();
         }
         }

smooks-config.xml

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd" >

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


</smooks-resource-list>

模块.xml

<module xmlns="urn:jboss:module:1.1" name="org.milyn">
  <resources>
    <resource-root path="milyn-smooks-all-1.4.jar"/>
</resources>

 <dependencies>
   <module name="javax.api"/>
  <module name="org.apache.commons.logging" />
  <module name="org.jaxen" />
   <module  name="org.hibernate" slot="3" />

  </dependencies>
</module>

例外 :

 java.io.IOException: Failed to access data stream for resource [/WEB-INF/smooks-config.xml]. Tried (in order):
10:10:14,113 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   File System: E:\WEB-INF\smooks-config.xml
10:10:14,114 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   File System: E:\jboss-as-7.1.1.Final\bin\WEB-INF\smooks-config.xml
10:10:14,117 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   Classpath: /WEB-INF/smooks-config.xml
10:10:14,125 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) 

10:10:14,126 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   at org.milyn.resource.URIResourceLocator.getResource(URIResourceLocator.java:130)

10:10:14,128 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   at org.milyn.resource.URIResourceLocator.getResource(URIResourceLocator.java:84)

10:10:14,129 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   at org.milyn.Smooks.addConfigurations(Smooks.java:307)

10:10:14,131 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   at org.milyn.Smooks.<init>(Smooks.java:151)

10:10:14,133 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   at com.manam.mobee.customer.MobeeCustomerHome.runSmooksTransform(MobeeCustomerHome.java:644)
4

2 回答 2

1

我对 smooks 了解不多,但我猜它正在使用它的类加载器来尝试在您发送它的路径上查找资源。由于您在模块中有 smooks,因此它使用的是自己的模块ModuleClassLoader,因此无法查看部署的类加载器中的内容。你最好使用java.io.InputStream构造函数。

如果您从 servlet 运行它,则可以使用javax.servlet.ServerContext.getResourceAsStream("WEB-INF/smokes-config.xml").

于 2013-03-20T16:22:00.293 回答
0

您需要将这部分中的相对路径更改Smooks smooks = new Smooks("/WEB-INF/smooks-config.xml")为绝对路径:)

于 2016-05-24T09:55:08.970 回答