为什么我收到java.lang.NullPointerException而不是soap响应?

时间:2013-11-13 12:28:44

标签: java soap soapui soap-client

我向服务器发送以下请求,当我复制请求并使用SOAPUI时,它显示正确的响应,

但是当我使用以下代码生成并发送它时,返回

java.lang.NullPointerException
第50行

sm.writeTo(out);

代码:

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPFactory soapFactory = SOAPFactory.newInstance();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();        
SOAPBody body = soapEnvelope.getBody();
header.removeNamespaceDeclaration(header.getPrefix());

soapEnvelope.addNamespaceDeclaration("v9", "ADDRESS OF SERVICE");

Name bodyName;
bodyName = soapFactory.createName("User");

SOAPElement getList = body.addChildElement("User", "v9");

Name childName;

getList.addChildElement("name", "v9").addTextNode("Alex");
getList.addChildElement("surname", "v9").addTextNode("Nicroid");   

message.writeTo(System.out); 

URL endpoint = new URL("ENDPOINT ADDRESS OF SERVER");
SOAPMessage response = connection.call(message, endpoint);

connection.close();

SOAPMessage sm = response;

ByteArrayOutputStream out = new ByteArrayOutputStream();

sm.writeTo(out); //java.lang.NullPointerException

System.out.println(out.toString());

的Maven

 <url>http://maven.apache.org</url>
  <build>
    <resources>
      <resource>
        <targetPath>META-INF</targetPath>
        <directory>src</directory>
        <includes>
          <include>jax-ws-catalog.xml</include>
          <include>wsdl/**</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.jvnet.jax-ws-commons</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.2.1</version>
        <dependencies>
          <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>webservices-api</artifactId>
            <version>1.4</version>
          </dependency>
        </dependencies>
        <configuration>
          <sourceDestDir>${project.build.directory}/generated-sources/jaxws-wsimport</sourceDestDir>
          <xnocompile>true</xnocompile>
          <verbose>true</verbose>
          <extension>true</extension>
          <catalog>${basedir}/src/jax-ws-catalog.xml</catalog>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jdom</groupId>
      <artifactId>jdom</artifactId>
      <version>1.1</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>com.sun.xml.ws</groupId>
      <artifactId>webservices-rt</artifactId>
      <version>1.4</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

4 个答案:

答案 0 :(得分:2)

如果您在第50行遇到NullPointerException,那是因为 st 为空。那是 connection.call(message,endpoint); 返回null。

假设您正在使用SOAPConnection的默认实现(com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnectionFactory - 见http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/xml/soap/SOAPConnectionFactory.java - 第47-48行) 方法调用(...)只能返回null:

  • 您收到的响应代码既不是HttpURLConnection.HTTP_INTERNAL_ERROR(500)也不是HttpURLConnection.HTTP_OK(200)
  • 响应代码正确但未返回回复消息

请参阅http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/com/sun/xml/internal/messaging/saaj/client/p2p/HttpSOAPConnection.java#HttpSOAPConnection.call%28javax.xml.soap.SOAPMessage%2Cjava.lang.Object%29第281至341行。

我无法继续进行分析,因为我无法重现这个问题。我建议你下载所涉及的java类的源代码,将它链接到你的调试器并在调试模式下执行它以查看你的用例中实际发生了什么。

答案 1 :(得分:1)

您正在使用的类SOAPMessage是抽象的,方法writeTo()也是如此。这意味着除了方法定义之外没有其他代码(类似于C中的函数原型)。内部(在方法的花括号内)是空的。要解决您的问题,您需要在新类中扩展SOAPMessage类,并为其中的方法提供实现。

public class myMessage extends SOAPMessage {
   public void writeTo(OutputStream out) throws SOAPException, IOException {
     // your code goes here
   }
  // All other methods in the superclass implemented here.
}

这里的抽象基础:http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

答案 2 :(得分:0)

SOAPConnection.call()是抽象的,不是吗?

你必须对类进行子类化,提供它的抽象方法的具体实现,然后实例化它。

否则你将最终得到NPE,因为你无法实例化一个抽象类。

答案 3 :(得分:0)

通过放置system.out.println(“soap message:”+ sm)来检查sm是否为空

如果响应为空  检查一下这个message.writeTo(System.out);声明将打印。 它还会提示你的肥皂要求是否形成良好

使用eclipse TCP / IP监视器或其他方式(启用跟踪日志记录)捕获要发送到端点的低级别http请求。很可能你的SOAP请求可能不是很好,因此服务器无法发回响应。