*
The moose likes Web Services and the fly likes JavaBean as parameter of a WebService operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "JavaBean as parameter of a WebService operation" Watch "JavaBean as parameter of a WebService operation" New topic
Author

JavaBean as parameter of a WebService operation

Marco Palazzini
Greenhorn

Joined: Apr 18, 2002
Posts: 13
HI
given that it is possible to pass as parameters of a WebService operation any object according to the JavaBean specification without having to define any custom mapping (SOAP 2.2 provides a bean serializer class)
my question is: is it possible for a JavaBean to have as member field another JavaBean?
example:
///////////////////////////////////////////////
class Person { //main JavaBean
String name;
String surname;
Car myCar; //contained JavaBean
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getSurname() {
return surname;
}
public void setCar(Car myCar) {
this.myCar = car;
}
public Car getCar() {
return myCar;
}
}
class Car {
String builder;
String model;
public void setBuilder(String builder) {
this.builder = builder;
}
public String getBuilder() {
return builder;
}
public void setModel(String model) {
this.model = model;
}
public String getModel() {
return model;
}
}
///////////////////////////////////////////////
I tried with WebSphere but it seems not to be possible.
Can you also please give me a link to the JavaBean specification?
Thanks
Marco
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I believe you can find the JavaBean specs on the sun site at java.sun.com
Bill
Mandan Happy
Ranch Hand

Joined: Dec 17, 2001
Posts: 65
Why Not?
Which implementationa re u using Suns or apaches.
Try GLUE from Mind Electric.It worked for me
-Mandan
Marco Palazzini
Greenhorn

Joined: Apr 18, 2002
Posts: 13
Thanks William I will look up Sun's web site.
Mandan, I have tried both with WebSphere Application Studio Developer from IBM and with xrpcc tool (WSDK) from Sun. In the first case (IBM) it raised an exception at run-time telling that it doesn't find a serializer for the 'inner' bean. In the second (SUN) the 'inner' bean seems to arrive null to the WebService.
But it's good to know it should work, that is what I was hoping. Now I will find the way to make it work...
Thanks
Marco
p.s. If you have time and patience this is what I tried on Sun's WSDK but it seems not to work, maybe you can tell me why:
HelloBean.java
//////////////////////////////////////////////////
package hello;
/**
*
* @author marco
* @version
*/
public class HelloBean {
int field0;
HelloInnerBean innerBean;

/** Creates new HelloBean */
public HelloBean() {
}
public void setField0(int field0) {
this.field0 = field0;
}
public int getField0() {
return field0;
}

public void setInnerBean(HelloInnerBean innerBean) {
this.innerBean = innerBean;
}
public HelloInnerBean getInnnerBean() {
return innerBean;
}

public String toString(){
return "HelloBean [field0="+field0+",innerBean="+innerBean+"]";
}
}
//////////////////////////////////////////////////
HelloInnerBean.java
//////////////////////////////////////////////////
package hello;
/**
*
* @author marco
* @version
*/
public class HelloInnerBean {
int field0;

/** Creates new HelloInnerBean */
public HelloInnerBean() {
}
void setField0(int field0) {
this.field0 = field0;
}
int getField0() {
return field0;
}

public String toString() {
return "HelloInnerBean [field0="+field0+"]";
}

}
//////////////////////////////////////////////////
HelloIF.java
//////////////////////////////////////////////////
package hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloIF extends Remote {
public String sayHello(String s) throws RemoteException;
public String sayHello(float f) throws RemoteException;

public String printBean(HelloBean helloBean) throws RemoteException;
}
//////////////////////////////////////////////////
HelloImpl.java
//////////////////////////////////////////////////
package hello;
public class HelloImpl implements HelloIF {
public String message = new String("Hello ");
public String sayHello(String s) {
return new String(message + s);
}
public String sayHello(float f) {
return new String(message + f);
}
public String printBean(HelloBean helloBean) {
return "HelloBean received: "+helloBean;
}

}
//////////////////////////////////////////////////
And finally the HelloClient.java
//////////////////////////////////////////////////
package hello;
public class HelloClient {
public static void main(String[] args) {
try {
HelloIF_Stub stub =
(HelloIF_Stub)(new HelloWorld_Impl().getHelloIF());
stub._setProperty(
javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY,
args[0]);
System.out.println(stub.sayHello("Duke!"));

HelloBean helloBean = new HelloBean();
HelloInnerBean helloInnerBean = new HelloInnerBean();
helloInnerBean.setField0(12);
System.out.println("Created inner bean: "+helloInnerBean);
helloBean.setField0(9);
helloBean.setInnerBean(helloInnerBean);
System.out.println(stub.printBean(helloBean));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
//////////////////////////////////////////////////
The final output is:
HelloBean received: HelloBean [field0=9,innerBean=null]
Marco Palazzini
Greenhorn

Joined: Apr 18, 2002
Posts: 13
ok, sorry guys, found spelling mistake
wrote "getInnnerBean()" with 3 "n" !!
Thanks, now it works :-)
Marco
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
So, finally, did it work with Websphere or Sun?


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Marco Palazzini
Greenhorn

Joined: Apr 18, 2002
Posts: 13
It works with both, good.
Thanks
Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaBean as parameter of a WebService operation
 
Similar Threads
getter method and setter method
can we cast an array ?
How can I map beans with static builder classes in CASTOR
HashMap and equals method overriding
please help me