user5157427 user5157427 - 1 month ago 6
Java Question

POST method that accept few arguments

Is it possible to write a @POST method that accept few arguments , some objects some primitive ? like this :

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.TEXT_PLAIN})
@Path("/post")
public String postMessage(Message msg,Car car,String s,int i) throws Exception{
}


if yes - how will the request body looks like ?

Thank you

Answer

It is possible by creating a wrapper container class for input. e.g.

class PostInput{
  private Message message,
  private Car car;
  private String s;
  private int i;

  // getters and setters for properties
}

then the signature of method will be

public String postMessage(PostInput input)

You'd need to form the corresponding json object in the client