2

I've just learned how to send a single complex object to a Jersey-based REST web service by means of JAXB mapping: http://jersey.java.net/nonav/documentation/latest/xml.html#d4e820

However, I'd like to do a bit more.

1) Can I have a method that accepts multiple complex objects, all annotated with JAXB? I mean something like:

@POST
public void setPlanetAndGalaxy ( Planet planet, Galaxy galaxy ) { ... }

Does Jersey allow for such a method? How would a client counter-part look like, using Jersey/Client? So far I've got the impression that I should define a wrapper like class GalaxyPlanet { planet, galaxy }, I don't like that very much.

2) Is it possible to have an array or a collection parameter for a POST method? Like in:

@POST
public void setPlanets ( Planet ... planets ) {...}

@POST
public void setPlanets ( Set<Planet> planets ) {...}

@POST
public void setPlanetsAndGalaxies ( Set<Planet> planets, Set<Galaxy> galaxies )

How would the client code look like in these three different cases?

Thanks in advance for any help. Marco.

zakmck
  • 2,715
  • 1
  • 37
  • 53
  • A resource should represent a single entity: are you sure you're trying to do something that isn't RESTful? – David Grant Sep 21 '12 at 17:25
  • No, I'm not sure :-) What I'm trying to do is to wrap calls like those above, which are already implemented for a programmatic API and command-line tools, with a REST web service in a standard/smooth way. I'm quite new to the latter. Thanks. – zakmck Sep 21 '12 at 17:32
  • Have you considered that the sets of entities might belong to a larger entity, e.g. System, Universe. – David Grant Sep 21 '12 at 18:13
  • Hi David, this is just an example, I wonder if in general it is possible to define a POST method accepting multiple complex objects and send such objects using the Jersey client library. You don't always want to define a single container like Universe. E.g., addRoute ( Planet p1, Planet p2, Route preferredRoute ) – zakmck Sep 22 '12 at 17:41
  • I know it's an example ;) But if you want to modify two distinct entities, I feel that might not be possible. – David Grant Sep 23 '12 at 15:45

1 Answers1

3

You should identify your resources and their relationships first. If you are going to deal with only galaxies and planets (for example) your REST API paths would be something like

/galaxies/ --> You can deal with list of galxies here
/galaxies/{oneGalaxyId}  --> One galaxy
/galaxies/{oneGalaxyId}/planets --> Planets of a galaxy
/galaxies/{oneGalaxyId}/planets/{planetId} --> One planet among the planets of a galaxy.

You would be able to deal with List as POST method parameter.
public void setPlanets ( List<Planet> planets ) {...}
Accepting multiple complex type parameters may not be possible.

basiljames
  • 4,777
  • 4
  • 24
  • 41
  • Thanks. In this example it makes sense. However, I'm interested in understanding what I can/can't do in general. By the way, setPlanet ( List planets ) doesn't seem so easy, I'm having the same problems reported in http://stackoverflow.com/questions/1603404/using-jaxb-to-unmarshal-marshal-a-liststring. – zakmck Sep 22 '12 at 17:30