wss4j开发包使用中读取SOAP文档出现的异常

java.lang.NullPointerException
 at org.apache.ws.security.util.WSSecurityUtil.findElement(WSSecurityUtil.java:199)
 at org.apache.ws.security.message.WSSecEncrypt.doEncryption(WSSecEncrypt.java:436)
 at org.apache.ws.security.message.WSSecEncrypt.doEncryption(WSSecEncrypt.java:399)
 at org.apache.ws.security.message.WSSecEncrypt.encryptForInternalRef(WSSecEncrypt.java:306)
 at org.apache.ws.security.message.WSSecEncrypt.build(WSSecEncrypt.java:264)
 at com.foresee.test.SOAPDocumentTest.testEncryptBody(SOAPDocumentTest.java:131)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at junit.framework.TestCase.runTest(TestCase.java:154)
 at junit.framework.TestCase.runBare(TestCase.java:127)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:118)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

该异常是再调用org.apache.ws.security.util.WSSecurityUtil.findElement(WSSecurityUtil.java:199)查找SOAP文档中的Body是出现的,wss4j中的源码:
    public static Node findElement(Node startNode, String name, String namespace) {

        /*
         * Replace the formely recursive implementation with a depth-first-loop
         * lookup
         */
        if (startNode == null) {
            return null;
        }
        Node startParent = startNode.getParentNode();
        Node processedNode = null;

        while (startNode != null) {
            // start node processing at this point
            if (startNode.getNodeType() == Node.ELEMENT_NODE
                    && startNode.getLocalName().equals(name)) {

该异常主要是因为startNode.getLocalName().equals(name)方法返回了null值,解决的办法是:
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  
  // 必须设置该值为true,否则将无法独取SOAP文档
  factory.setNamespaceAware(true);

 此时构建的Document就可以正常使用Node.getLocalName()方法了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值