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()方法了.