Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving all POST data parameters into map with Java Jersey

I'm trying to gather all parameters of an HTTP POST request into a map, however it's not working.

With a GET, I used:

@Context
private HttpServletRequest request;

and

@GET
@Path("/{uuid}/invoke/{method}")
public Response invokeMethod (
    @PathParam("uuid") String uuid,
    @PathParam("method") String methodz
) {
    Map<String, String[]> params = request.getParameterMap();

and it returned the map. However, when sending form data via a POST instead of inline with the URL, the map is being returned as NULL.

Is there a similar way to retrieve all the parameters at once using a POST?

EDIT:

My data is being submitted as a serialized JSON, so using a cURL statement as an example:

curl --data "firstname=john&lastname=smith" http://localhost:8080/uuid1/apitest/method1

Ideally, I'd want to get a hashmap of something like:

ParamMap["firstname"] = "john"
ParamMap["lastname"] = "smith"

Also, the parameters won't be static, so this cURL:

curl --data "job=construction" http://localhost:8080/uuid2/apitest/method2

would result in:

ParamMap["job"] = "construction"
like image 595
ev0lution37 Avatar asked May 09 '26 09:05

ev0lution37


1 Answers

Finally got this resolved using this thread:

Jersey: Consume all POST data into one object

My new function is:

@POST
@Path("/{uuid}/invoke/{method}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response invokeMethod (
    MultivaluedMap<String,String> params,
    @PathParam("uuid") String uuid,
    @PathParam("method") String method
) {

Variable 'params' is now a map containing form data key/value pairs.

like image 155
ev0lution37 Avatar answered May 10 '26 21:05

ev0lution37



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!