I am not using JSON or anything like that. I have a simple form to upload a file and I want to read the parameters of the form. The code below is not working as expected. It will not show any parameters.
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{appNum}/{docId}/file")
public Response uploadDocFile(
@PathParam("appNum") String appNum,
@PathParam("docId") String docId,
@Context HttpServletRequest req)
{
try {
log.info("POST Parameters:");
Enumeration e = req.getParameterNames();
while(e.hasMoreElements())
{
Object key = e.nextElement();
log.info("Key: " + key);
log.info("Val: " + req.getParameter(key.toString()));
}
} catch (Exception e) {
e.printStackTrace();
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(new StatusResponse(e)).build();
}
return Response.ok().build();
}
FYI, You need to use @FormParam. Also make sure INPUT HTML types are using name= not id=.
I have the same problem. Using
@FormParam
annotation for individual parameters works, but reading them fromHttpServletRequest
injected through@Context
doesn't. I also tried to get the request object/parameters through Guice usingProvider<HttpServletRequest>
and@RequestParameters<Map<String, String[]>>
. In both cases there were no post parameters.However, it is possible to get a map of parameters by adding a
MultivaluedMap<String, String>
parameter to resource method. Example:If you are using Jersey RESTful API in JAVA you can look for Parameter Annotations (@*Param)
Example:
Dependency:
Code:
List of annotations: @MatrixParam, @HeaderParam, @CookieParam, @FormParam, @QueryParam, @PathParam
At some point of time Jersey ContainerServlet (or other Jersey object during request processing) calls request.getInputStream() or request.getReader() which set 'usingInputStream' or 'usingReader' to TRUE. This state prevents populating of parameters map inside the request object. Something like this:
Try putting a break point at the very first entry point (beginning of Jersey ServletContainer.service() method) of your application and evaluate request.getParametersMap() call. You'll get your parameters.