I have a class like this:
class Customer { private int id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
and I have a glass fish web service:
i want to know it is possible to send a customer object using get
(i know i can do this in post, but in get … i don’t know)
this is what i tried:
@GET @Path("/test") @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_OCTET_STREAM) public String test(@QueryParam("customer") Customer customer) { return "Done " + customer.getId(); }
then i call it like this:
..../test?id=4&name=william
I know that is wrong, but i don’t know the correct way, and i don’t know if that is even possible using get
Answer
@QueryParam
should be used for each individual parameter. For instance
/cusomters?name=hello&id=1 @GET @Produces(...) public Response get(@QueryParam("name") String name, @QueryParam("id") int id)
If you want put it into a bean, you can use @BeanParam
, which allows you to put arbitrary @XxxParam
s into a bean. For example
class Customer { @QueryParam("name") private String name; @QueryParam("id") private int id; // getters/setters } @GET public Response get(@BeanParam Customer customer)
But do keep in mind REST principles. To create a customer resource, it should be done with POST. Also be considerate of security concerns. You do not want private user information in URLs.