0

I am currently working on a project that uses JAX-WS webservices in Java. The global topic is this : the user creates locally an object, let's say an Agent. He calls a first webservice and passes its Agent to the webservice. The webservice treats the Agent (modifies its properties : e.g. lifepoints), and passes it to another webservice. This call is made from the first webservice, so the user has nothing to do in the process.

After a chain of several webservices, the user retrieves the Agent that has been modified.

The aim of my project is to design 2 parts:

  • a framework that specifies the behaviour previously described : webservices, Agents and the process of migration
  • a demo application using my framework. The main difference is the addition of a GUI and a new class Avatar, that extends Agent. So the migration process is still being done "by the framework", with Agent objects.

The following code shows a simple example of how I call my webservice, host my Avatar, then retrieves the agent from the service :

// connection to the server
URL endpoint= new URL("http://SERVER/tomcat/KiwiBidonDynamique/ServiceWebBidonDeDadou?wsdl");
QName serviceName=new QName("http://avatar/","ServeurKiwiBidonService");
Service service = Service.create(endpoint, serviceName);
WebService port = service.getPort(WebService.class);


Avatar myAvatar = new Avatar(1, "Jack the Ripper");
port.hostAgent(myAvatar);

// some process on the service...

Avatar myAvatarTransformed = (Avatar) port.getAgent("AgentNumberOne");

When I do that, I get an exception on the final line :

Exception in thread "main" java.lang.ClassCastException: agent.Agent cannot be cast to avatar.Avatar

After a lot of log reading, I guess the reason is the way the webservice works. When being called, my Avatar given in parameter is marshalled in my JVM then unmarshalled on the service, but the service only constructs an Agent when it unmarshalles. Doing so, it truncates the data specific to the Avatar. Then when I try to retrieve my Agent from the service, it cannot be cast to an Avatar.

Is there a way to keep the Avatar information while processing as an Agent on the service ? Can I write my own marshalling/unmarshalling somehow ?

Thanks a lot.

David Ferrand
  • 5,357
  • 1
  • 33
  • 43

1 Answers1

0

If your webservice has Agent element defined as incoming data, then no it is not possible to unmarshall it into an inherited class. I guess it would be possible to write your own marshaller but it is not as easy as it sounds (I would advise against it). Either write a separate WS for each class (messy) or make the incoming data have an element that can store additional structures, like type:any (also messy). The truth is WS are not exactly OO.

Xargos
  • 633
  • 8
  • 19
  • Thanks a lot. These were the options I was beginning to think of... I've been trying to implement custom marshalling with a XmlAdapter : http://stackoverflow.com/a/7282020/2291104 – David Ferrand Apr 24 '13 at 14:21