0

I'm sending json-formatted data to my Java server via http requests. I've had great success in receiving the requests with functions like

Boolean deleteUsers(List<Long> userIds) {
    // ...
    return true;
}

I'm using RESTEasy on a Java server, and it cheerfully converts the payload of the request into this List<Long> that's so convenient.

Now I want to send a String and a list of numbers! Ideally, my receiving function would look something like

Boolean deleteUsers(String string, List<Long>userIds) {
    // ....
    return true;
}

Alas, RESTEasy doesn't seem to know what I mean, and chokes on the payload.

How can I receive multiple types of data from a payload?

Riley Lark
  • 20,660
  • 15
  • 80
  • 128
  • Hassle of a workaround suggested at http://stackoverflow.com/questions/5726583/spring-rest-multiple-requestbody-parameters-possible – Riley Lark Oct 19 '11 at 21:10

2 Answers2

3

Depending on your specific use case, you might simply add the first parameter ("string") to the @Path annotation such as

@POST
@Path("{string:.*}")
@Consume(MediaType.APPLICATION_JSON)
@Produce(MediaType.APPLICATION_JSON)
Boolean deleteUsers(@PathParam("string") String string, List<Long>userIds) {
...
}

This would result in the following URL form:

/service/<string>/

with the payload containing your userid list (as json).

As you might realize, @POST method type is suggested since the service construction method won't be idempotent. Otherwise, @DELETE would have been favoured.

pdeschen
  • 1,369
  • 15
  • 17
2

It seems that a wrapper object is needed.

class TwoObjectDTO {
    String string;
    List<Long> listOfNumbers;
}

@POST
Boolean deleteUsers(TwoObjectDTO object) {
    ...
}
Riley Lark
  • 20,660
  • 15
  • 80
  • 128