NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
apriebe
Joined: 01 Nov 2010 Posts: 6
|
Posted: Fri Nov 05, 2010 8:23 pm Post subject: Web Service returning partial values, or nothing at all. |
|
|
I'm learning how to work with Web Services and Java. This is my first example.
Currently there are three web methods in my web service. One receives two ints and returns their sum. This works perfectly fine.
My problem comes in when I try to pass back an array or a hashmap from the web service. Currently the other two web methods construct an array and a hashmap to be returned. This is because I'm trying to figure out the best way to return my data.
When I call the web service in my .jsp page I get nothing back from the web service. At one point I was able to pass back the first value in a single dimension array, but none of the other values passed back with it.
My question is: how do you return an array, or some other in-depth form of variable like a hashmap or vector, using a web service and display its contents. I feel because I'm so new to Java that I'm missing some minor step.
I've created web services in PHP and you have to specify in the WSDL what data types you're going to pass back and forth, but I don't see that in the WSDL that get's auto-generated by Java. I think my web service is actually working as designed in that respect, I just don't know what to code to make it work the way I want it to.
Also, I think I'm deploying correctly. When I update my web service I do a clean and build, then deploy. I then refresh the WSDL on the web service client, clean and build, then deploy. So I believe I'm updating everything properly when I try new code.
Does anyone have a good example of a web service that returns an array? My ultimate goal is to pull data from a DB, put it in an array or hashmap and return that to the client.
My dev box is running NetBeans6.9.1, Tomcat6, JDK1.6.0_22.
Web Service Code on Tomcat6:
| Code: |
import java.util.HashMap;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.lang.String;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.sql.*;
import javax.sql.*;
/**
*
* @author apriebe
*/
@WebService()
public class PulseWS {
/**
* Web service operation
*/
@WebMethod(operationName = "add")
public int add(@WebParam(name = "i")
int i, @WebParam(name = "j")
int j) {
int k = i + j;
return k;
}
/**
* Web service operation
*/
@WebMethod(operationName = "getOrganizationInfo")
public String[][] getOrganizationInfo(@WebParam(name = "organizationInfo")
String[][] organizationInfo) {
organizationInfo[0][0] = "86";
organizationInfo[0][1] = "Gottlieb Memorial";
organizationInfo[0][2] = "GOTLB";
return organizationInfo;
}
/**
* Web service operation
*/
@WebMethod(operationName = "hmTest")
public HashMap hmTest(@WebParam(name = "hm")
HashMap hm) {
//TODO write your implementation code here:
hm.put("ID", 86);
hm.put("DisplayName", "Gottlieb Memorial");
hm.put("OrganizationCode", "GOTLB");
return hm;
}
} |
Web Service Client on Tomcat6:
| Code: | <%@page import="java.util.Arrays" %>
<%@page import="java.lang.String" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Pulse WS</h1>
<h2>Integer Adding Service</h2>
<%-- start web service invocation --%><hr/>
<%
try {
unisyn.apriebe.pulse.PulseWSService service = new unisyn.apriebe.pulse.PulseWSService();
unisyn.apriebe.pulse.PulseWS port = service.getPulseWSPort();
// TODO initialize WS operation arguments here
int i = 22;
int j = 28;
out.println("Integers being added are "+i+" and "+j+".<br/>");
// TODO process result here
int result = port.add(i, j);
out.println("Result = "+result);
} catch (Exception ex) {
out.println("Exception" + ex);
}
%>
<%-- end web service invocation --%><hr/>
<h2>Array Example</h2>
<%-- start web service invocation --%><hr/>
<%
try {
unisyn.apriebe.pulse.PulseWSService service = new unisyn.apriebe.pulse.PulseWSService();
unisyn.apriebe.pulse.PulseWS port = service.getPulseWSPort();
// TODO initialize WS operation arguments here
java.util.List<java.lang.String> organizationInfo = null;
// TODO process result here
java.util.List<java.lang.String> result = port.getOrganizationInfo(organizationInfo);
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
<%-- end web service invocation --%><hr/>
<h2>HashMap Example</h2>
<%-- start web service invocation --%><hr/>
<%
try {
unisyn.apriebe.pulse.PulseWSService service = new unisyn.apriebe.pulse.PulseWSService();
unisyn.apriebe.pulse.PulseWS port = service.getPulseWSPort();
// TODO initialize WS operation arguments here
unisyn.apriebe.pulse.HashMap hm = null;
// TODO process result here
unisyn.apriebe.pulse.HashMap result = port.hmTest(hm);
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
<%-- end web service invocation --%><hr/>
</body>
</html> |
The auto-generated WSDL:
| Code: |
This XML file does not appear to have any style information associated with it. The document tree is shown below.
−
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2-hudson-740-.
-->
−
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2-hudson-740-.
-->
−
<definitions targetNamespace="http://pulse.apriebe.unisyn/" name="PulseWSService">
−
<types>
−
<xsd:schema>
<xsd:import namespace="http://pulse.apriebe.unisyn/" schemaLocation="http://localhost:8080/PulseWSApplication/PulseWS?xsd=1"/>
</xsd:schema>
−
<xsd:schema>
<xsd:import namespace="http://jaxb.dev.java.net/array" schemaLocation="http://localhost:8080/PulseWSApplication/PulseWS?xsd=2"/>
</xsd:schema>
</types>
−
<message name="getOrganizationInfo">
<part name="parameters" element="tns:getOrganizationInfo"/>
</message>
−
<message name="getOrganizationInfoResponse">
<part name="parameters" element="tns:getOrganizationInfoResponse"/>
</message>
−
<message name="hmTest">
<part name="parameters" element="tns:hmTest"/>
</message>
−
<message name="hmTestResponse">
<part name="parameters" element="tns:hmTestResponse"/>
</message>
−
<message name="add">
<part name="parameters" element="tns:add"/>
</message>
−
<message name="addResponse">
<part name="parameters" element="tns:addResponse"/>
</message>
−
<portType name="PulseWS">
−
<operation name="getOrganizationInfo">
<input wsam:Action="http://pulse.apriebe.unisyn/PulseWS/getOrganizationInfoRequest" message="tns:getOrganizationInfo"/>
<output wsam:Action="http://pulse.apriebe.unisyn/PulseWS/getOrganizationInfoResponse" message="tns:getOrganizationInfoResponse"/>
</operation>
−
<operation name="hmTest">
<input wsam:Action="http://pulse.apriebe.unisyn/PulseWS/hmTestRequest" message="tns:hmTest"/>
<output wsam:Action="http://pulse.apriebe.unisyn/PulseWS/hmTestResponse" message="tns:hmTestResponse"/>
</operation>
−
<operation name="add">
<input wsam:Action="http://pulse.apriebe.unisyn/PulseWS/addRequest" message="tns:add"/>
<output wsam:Action="http://pulse.apriebe.unisyn/PulseWS/addResponse" message="tns:addResponse"/>
</operation>
</portType>
−
<binding name="PulseWSPortBinding" type="tns:PulseWS">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
−
<operation name="getOrganizationInfo">
<soap:operation soapAction=""/>
−
<input>
<soap:body use="literal"/>
</input>
−
<output>
<soap:body use="literal"/>
</output>
</operation>
−
<operation name="hmTest">
<soap:operation soapAction=""/>
−
<input>
<soap:body use="literal"/>
</input>
−
<output>
<soap:body use="literal"/>
</output>
</operation>
−
<operation name="add">
<soap:operation soapAction=""/>
−
<input>
<soap:body use="literal"/>
</input>
−
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
−
<service name="PulseWSService">
−
<port name="PulseWSPort" binding="tns:PulseWSPortBinding">
<soap:address location="http://localhost:8080/PulseWSApplication/PulseWS"/>
</port>
</service>
</definitions> |
Output from .jsp:
| Code: |
Pulse WS
Integer Adding Service
Integers being added are 22 and 28.
Result = 50
Array Example
HashMap Example
|
|
|
| Back to top |
|
 |
Czar
Joined: 04 Nov 2010 Posts: 12 Location: GE
|
Posted: Sun Nov 07, 2010 2:45 pm Post subject: |
|
|
| Look at your server's log, I bet, you'll see NullPointerException traces. You pass null values from client to your ws operations and try to perform operations on those null values. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|