5

次のリクエストをサーバーに送信しています。リクエストをコピーして 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());

メイヴン

 <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

4 に答える 4

1

メソッド writeTo() と同様に、使用しているクラス SOAPMessage は抽象クラスです。これは、メソッド定義以外のコードがないことを意味します (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

于 2013-11-21T01:27:44.217 に答える
0

system.out.println(" soap message:" +sm) を配置して、sm が null かどうかを確認します。

応答が null の場合、この message.writeTo(System.out) を確認します。ステートメントが印刷されます。また、SOAP リクエストが適切に形成されているかどうかについてのヒントも提供します。

Eclipse TCP/IP モニターを使用するか、その他の手段 (トレース ロギングを有効にする) を使用して、エンドポイントに送信する低レベルの http 要求をキャプチャします。ほとんどの場合、SOAP リクエストの形式が正しくない可能性があるため、サーバーが応答を返すことができません。

于 2013-11-21T06:34:15.013 に答える