As Ian mentioned, this is quite a bit easier with JAXWS/JAXB 2.1.  With 
the 2.1 snapshots, you can add

@XmlSeeAlso(FooBar.class) to the IFake interface or to the Foo object to 
allow the runtime to know that the FooBar class should also be added to 
the JAXBContext.  

Alternatively, for CXF 2.0.5 and 2.1, if Foo.java and FooBar.java are in 
the same package, you can add a file called "jaxb.index" to the package 
that just contains a single line:
FooBar
(don't put a package qualifier there as the jaxb.index only loades 
classes from the same package)  The JAXB databinding will pick that up 
and load those classes as well.

Dan


On Sunday 20 April 2008, Rafael Ribeiro wrote:
> Hi all,
>
>  I have an webmethod that returns an arbitrary class. The problem is,
> on some of its executions it might return some subclass of the class
> that is expressed on the method signature. Instead of getting the
> actual class that was serialized from the webservice the client is
> getting the class that is expressed on the method. Do I have to
> specify anything on the classes that I need to serialize or on the
> method in order for it to correct serialize the expected class?
> I am sending a sample I did to reproduce the problem described above
> (it is deployed on tomcat using CXFNonSpringServlet and service is
> registered using an startupservlet):
>
> -------------------------------- IFake.java
> -------------------------------------
>
> package fake;
>
> import javax.jws.WebService;
>
> @WebService
> public interface IFake {
>     public Foo fooOp();
> }
>
> -------------------------------- FakeImpl.java
> -------------------------------------
>
> package fake;
>
> public class FakeImpl implements IFake {
>
>     public Foo fooOp() {
>         return new FooBar();
>     }
>
> }
>
>
> -------------------------------- FakeCli.java
> -------------------------------------
>
> import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
>
> import fake.IFake;
>
> public class FakeCli {
>     private static IFake fakeClient;
>
>     public static void main(String[] args) {
>         System.out.println(getFakeClient().fooOp());
>     }
>
>     public static IFake getFakeClient() {
>         if (fakeClient == null) {
>             JaxWsProxyFactoryBean factory = new
> JaxWsProxyFactoryBean(); factory.setServiceClass(IFake.class);
>             factory
>                     .setAddress("
> http://localhost:8080/mywebapp/services/fake";);
>             fakeClient = (IFake) factory.create();
>         }
>         return fakeClient;
>     }
> }
>
> -------------------------------- Foo.java
> -------------------------------------
>
> package fake;
>
> public class Foo {
>     private String foo;
>
>     public String getFoo() {
>         return foo;
>     }
>
>     public void setFoo(String foo) {
>         this.foo = foo;
>     }
>
> }
>
> -------------------------------- FooBar.java
> -------------------------------------
>
> package fake;
>
> public class FooBar extends Foo {
>     private String fooBar;
>
>     public String getFooBar() {
>         return fooBar;
>     }
>
>     public void setFooBar(String fooBar) {
>         this.fooBar = fooBar;
>     }
>
> }
>
> ----------------------------------------------------------------------
>--
>
>  This webservice is registered by this call on the startupservlet:
>         Endpoint.publish("/fake", new FakeImpl());
>
> and the result of the execution of FakeCli is something like:
> [EMAIL PROTECTED]
>
> If I try to cast it to FooBar I get a ClassCastException, as expected
> since this class was really instantiated as Foo instead of FooBar



-- 
J. Daniel Kulp
Principal Engineer, IONA
[EMAIL PROTECTED]
http://www.dankulp.com/blog

Reply via email to