I am sending the following request to the server, when I copy the request and use the SOAPUI it shows the correct response,
but when I use the following code to generate and send it, returns
java.lang.NullPointerException
on line 50, which is
sm.writeTo(out);
code:
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>
check whether sm is null by placing system.out.println(" soap message:" +sm)
if response is null check what this message.writeTo(System.out); statement will print. It will also give hint about your soap request is well formed or not
capture the low level http request you are sending to the endpoint using eclipse TCP/IP monitor or through other means (enable trace logging). most probably your SOAP request might not be well formed so, server failing to send response back.
If you are having NullPointerException at line 50 it's because st is null. That is connection.call(message, endpoint); is returning null.
Assuming you are using the default implementation of SOAPConnection (com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnectionFactory - See http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/xml/soap/SOAPConnectionFactory.java - Lines 47-48) There are just a few reasons method call(...) can return null:
See 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 Lines 281 to 341.
I can't go any further with the analysis because I'm not able to reproduce the issue. I suggest you download source code of involved java classes, link it to your debugger and execute it in debug mode to see what is actually happening in your usecase.
SOAPConnection.call() is abstract is it not?
You must subclass the class, provide concrete implementations of it's abstract methods and then instantiate it.
Otherwise you will end up with NPEs as you cannot instantiate an abstract class.
The class you are using, SOAPMessage, is abstract, as is the method writeTo(). This means there is no code other than the method definition (similar to a function prototype in C). The insides (inside the curly braces of the method) are empty. To fix your issue, you need to extend the SOAPMessage class in a new class and provide implementation for the methods therein.
Basics of abstraction here: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html