Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaBean as parameter of a WebService operation

 
Marco Palazzini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you can find the JavaBean specs on the sun site at java.sun.com
Bill
 
Mandan Happy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Not?
Which implementationa re u using Suns or apaches.
Try GLUE from Mind Electric.It worked for me
-Mandan
 
Marco Palazzini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, sorry guys, found spelling mistake
wrote "getInnnerBean()" with 3 "n" !!
Thanks, now it works :-)
Marco
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, finally, did it work with Websphere or Sun?
 
Marco Palazzini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works with both, good.
Thanks
Marco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic