views:

55

answers:

1

I found that I was unable to return collections from my JAX-WS Web Service.

I appreciate that the Java Collections API may not be supported by all clients, so I switched to return an array, but I can't seem to do this either.

I've set up my web service as follows:

@WebService
public class MyClass {
  public ReturnClass[] getArrayOfStuff() {
    // extremely complex business logic... or not
    return new ReturnClass[] {new ReturnClass(), new ReturnClass()};
  }
}

And the ReturnClass is just a POJO. I created another method that returns a single instance, and that works. It just seems to be a problem when I use collections/arrays.

When I deploy the service, I get the following exception when I use it:

javax.xml.bind.MarshalException - with linked exception: [javax.xml.bind.JAXBException: [LReturnClass; is not known to this context]

Do I need to annotate the ReturnClass class somehow to make JAX-WS aware of it? Or have I done something else wrong?

+1  A: 

I am unsure of wheter this is the correct way to do it, but in one case where I wanted to return a collection I wrapped the collection inside another class:

@WebService
public class MyClass {
    public CollectionOfStuff getArrayOfStuff() {
        return new CollectionOfStuff(new ReturnClass(), new ReturnClass());
    }
}

And then:

public class CollectionOfStuff {
   // Stuff here
   private List<ReturnClass> = new ArrayList<ReturnClass>();
   public CollectionOfStuff(ReturnClass... args) {
       // ...
   }
}

Disclaimer: I don't have the actual code in front of me, so I guess my example lacks some annotations or the like, but that's the gist of it.

waxwing
+1 That's exactly what I've just tried. It works, but I'm also not convinced it's the 'correct' way.
Brabster