Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rest using @QueryParam with @POST or @PUT

Tags:

java

rest

jersey

I'm using jersey API for some REST web services with apache Tomcat. I need to pass more than one parameter to a method so I decided to use @QueryParam annotation like this :

@GET
@Path("/date")
@Produces(MediaType.APPLICATION_JSON)
public Response getDate(@QueryParam("id") String Id, @QueryParam("inDate") String inDate)
{
...
}

when I call it like this everything works. But when I use annotaitions @POST or @PUT instead of @GET then it shows an error:

HTTP Status 405 - Method Not Allowed

message Method Not Allowed

description The specified HTTP method is not allowed for the requested resource (Method Not Allowed).

Is it possible to user this with POST or PUT and how?

Any help is appreciated.

like image 458
gajo Avatar asked Nov 06 '11 19:11

gajo


People also ask

When would you use query string parameters vs route parameters?

' in the URL, path parameters come before the question mark sign. Secondly, the query parameters are used to sort/filter resources. On the other hand, path parameters are used to identify a specific resource or resources. You can't omit values in path parameters since they are part of the URL.

Should a PUT request have query parameters?

Is it OK to use query parameters in a PUT request? Absolutely. Query parameters are just another piece of the resource identifier.

Can Rest POST have query parameters?

POST should not have query param. You can implement the service to honor the query param, but this is against REST spec. "Why do you pass it in a query param?" Because, like for GET requests, the query parameters are used to refer to an existing resource.


1 Answers

Yes, you just need to make the request using POST or PUT. This can't be done via the URL - you'd need a specific client (for example the http resource firefox plugin) that can send requests with methods other than GET. Note that for POST you can also use @FormParam

like image 139
Bozho Avatar answered Sep 19 '22 00:09

Bozho