Trying to test my first SOAP-driven Web Service
clarkvalentine Oct 16, 2008 2:50 PMAfter the excellent assistance I received the other day, I have a very simple web service deployed:
My POJO is:
package com.sample.test; @javax.jws.WebService public class HelloService { @javax.jws.WebMethod public String sayHello( @javax.jws.WebParam String name ) { return "Hello, " + name; } }
The web.xml is:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> - <servlet> <servlet-name>HelloService</servlet-name> <servlet-class>com.sample.test.HelloService</servlet-class> </servlet> - <servlet-mapping> <servlet-name>HelloService</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
The WSDL produced when I deploy this is:
<?xml version="1.0" encoding="UTF-8"?> <definitions name="HelloServiceService" targetNamespace="http://test.sample.com/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://test.sample.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> <types> <xs:schema targetNamespace="http://test.sample.com/" version="1.0" xmlns:tns="http://test.sample.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="sayHello" type="tns:sayHello"/> <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/> <xs:complexType name="sayHello"> <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="sayHelloResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> </types> <message name="HelloService_sayHelloResponse"> <part name="sayHelloResponse" element="tns:sayHelloResponse"> </part> </message> <message name="HelloService_sayHello"> <part name="sayHello" element="tns:sayHello"> </part> </message> <portType name="HelloService"> <operation name="sayHello" parameterOrder="sayHello"> <input message="tns:HelloService_sayHello"> </input> <output message="tns:HelloService_sayHelloResponse"> </output> </operation> </portType> <binding name="HelloServiceBinding" type="tns:HelloService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="HelloServiceService"> <port name="HelloServicePort" binding="tns:HelloServiceBinding"> <soap:address location="http://HootyOwl.sample.com:8080/helloService/hello"/> </port> </service> </definitions>
Now, I'm trying to test with the following ugly client code:
import java.net.*; import java.io.*; public class SOAPClient01 { public final static String DEFAULT_SERVER = "http://hootyowl.sample.com"; public static final String DEFAULT_PORT = "8080"; public static final String SERVICE_NAME = "helloService/hello"; public static final String SOAP_ACTION = "hootyowl.sample.com"; public static void main(String[] args) { try { //URL u = new URL(DEFAULT_SERVER+"/"+SERVICE_NAME); URL u = new URL(DEFAULT_SERVER+":"+DEFAULT_PORT+"/"+SERVICE_NAME); URLConnection uc = u.openConnection(); HttpURLConnection connection = (HttpURLConnection) uc; connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("SOAPAction", SOAP_ACTION); OutputStream out = connection.getOutputStream(); Writer wout = new OutputStreamWriter(out); wout.write("<?xml version='1.0'?>\r\n"); wout.write("<SOAP-ENV:Envelope "); wout.write("xmlns:SOAP-ENV="); wout.write( "'http://schemas.xmlsoap.org/soap/envelope/' " ); wout.write("xmlns:tns='test.sample.com'"); wout.write("xmlns:xsi="); wout.write( "'http://www.w3.org/2001/XMLSchema-instance'>\r\n"); wout.write(" <SOAP-ENV:Body> \r\n"); wout.write(" <tns:sayHello>"); // wout.write("\t<tns:arg0>Clark</tns:arg0>\r\n" // + "</tns:sayHello>\r\n"); wout.write("Clark"); wout.write("</tns:sayHello>"); wout.write(" </SOAP-ENV:Body>\r\n"); wout.write("</SOAP-ENV:Envelope>\r\n"); wout.flush(); wout.close(); InputStream in = connection.getInputStream(); int c; while ((c = in.read()) != -1) System.out.write(c); in.close(); } catch (IOException e) { System.err.println(e); } } // end main }
The SOAP produced looks like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tns='test.sample.com'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <SOAP-ENV:Body> <tns:sayHello>Clark</tns:sayHello> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
I'm receiving the following output:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://hootyowl.sample.com:8080/helloService/hello
(I'm using JBoss 4.2.3 GA on RedHat Enterprise 4, JDK 1.6.)
Two questions: 1) Is this error related to my code, or am I not communicating with the server correctly? 2) What am I doing wrong, if I can ask so general a question?
Thanks in advance!
- Clark, still a newbie