I have to update a Person record having firstName and lastName. User should be able to change it from html form and on submit it should be updated.
Here is my code.
@PUT
@Path("/{userId}")
public Response updatingResource(@FormParam("firstName") String firstName, @FormParam("lastName ") String lastName , @PathParam("userId") String userId){
System.out.println(firstName);
System.out.println(lastName);
return Response.ok().build();
}
the SOP statements prints null. I have been using Mozilla Firefox's Poster plugin to send PUT request.
I also tried by annotating it with @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
but still it is printing null for each values.
How to write and call PUT method that receives these three values. I stumble around lot and found people were asking to use JSON or XML. How can I consume JSON? I would be very greatfull if someone help me to write REST method to update a resource
If I send PUT request using Firefox's RESTClient and Google's rest-client I am able to get the form parameters. Both this tool has something like body section where I placed firstName=Amit&lastName=Patel
. Also I added header Content-Type
as application/x-www-form-urlencoded
.I think Firefox's Poster is buggy. Can anyone suggest me is there any other way I should validate the code or I can trust on first two REST clients?
1 Answer. Explanation: @MatrixParam is the annotation that binds the parameter passed to method to a HTTP matrix parameter in path, while @QueryParam binds to a query parameter, @PathParam binds to a value and @HeaderParam binds to the HTTP header in the path.
JAX-RS is an specification (just a definition) and Jersey is a JAX-RS implementation. Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides its own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development.
The @DELETE annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP DELETE requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
In addition to annotating your method with @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
, you must send application/x-www-form-urlencoded
as a content-type. Did you do it?
Edited: You can use FormParams only with POST:
SRV.4.1.1 When Parameters Are Available The following are the conditions that must be met before post form data will be populated to the parameter set:
- The request is an HTTP or HTTPS request.
- The HTTP method is POST.
- The content type is application/x-www-form-urlencoded.
- The servlet has made an initial call of any of the getParameter family of methods on the request object. If the conditions are not met and the post form data is not included in the parameter set, the post data must still be available to the servlet via the request object’s input stream. If the conditions are met, post form data will no longer be available for reading directly from the request object’s input stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With