1

I am using Jersey 2.3.1 on Glassfish 4.

My resource method is similar to the following:

        @POST
        @Consumes("application/x-www-form-urlencoded")
        @Path("/update")
        public Response update(MultivaluedMap<String, String> formParams){
    //business logic
    //return appropriate Response object
    }

I always get formParams.size() as zero. Why the submitted form parameters are not available in the MultivaluedMap object?

The following warning message in the server log:

WARNING: A servlet request to the URI http://localhost:8080/myApp/resource/update contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.

(I tested before and after disabling all Servlet filters. I am not using any Jersey filters)

siva636
  • 16,109
  • 23
  • 97
  • 135

1 Answers1

2

You get this message if request body with form data was already consumed by calling HttpServletResponse.getParameter(paramName). This can happen if any registered servlet filter called this method. Jersey ContainerRequestFilter cannot influence it. So I suggest to investigate the configuration of your deployment (web.xml). I have tested injecting Form entity with Jersey 2.5-SNAPSHOT and Glassfish 4 night build (glassfish-4.0.1-b04-12_04_2013) and it works.

If request body is already consumed you can still use form parameters but you cannot inject them as an entity (like in your code). If parameters are consumed, you can inject parameters using @FormParam JAX-RS annotation:

@POST
@Consumes("application/x-www-form-urlencoded")
public String postForm(@FormParam("paramKey") String paramValue) {
    return paramValue;
}
Miroslav Fuksa
  • 461
  • 3
  • 6