users@jersey.java.net

_at_FormParam, Servlets and x-www-form-urlencoded

From: Samuel Le Berrigaud <samuel.lb_at_gmail.com>
Date: Mon, 19 Jan 2009 12:12:14 +1100

Hi all,

we are experiencing an interesting issue and I hoped that someone
would have had a similar problem and could help. Here it is.

We deployed a Jersey servlet
(com.sun.jersey.spi.container.servlet.ServletContainer) into our
application. We have a service that makes use for @FormParam.
So when using the Jersey client we do something like:

final Form formData = new Form();
formData.add("param1", "value1");
final MyResource resource =
Client.create().resource("http://localhost:8080/rest").path("myresource").post(MyResource.class,
formData);

on the server side we try to get the value of the param1 like so:
@FormParam("param1") String param1

Nothing complicated here. However on the server side param1 is always
when we use that code. Here is the reason why.

The Jersey client generated request has its content type set to
"application/x-www-form-urlencoded" which is the correct content type
for HTTP POSTs that have a body of url encoded params. However in our
application we have several filters, some of which access the request
parameters through one of the ServletRequest#getParameter* methods.
This means that the application server (I tested on Tomcat 5.5 and
Jetty 6.1) will consume the request input stream to access the
parameters and leave it empty.
So when Jersey tries to parse the request body it finds it to be
empty. Hence our param1 being null.

I understand that Jersey needs to access the request body to parse
those parameters as JAX-RS differentiates FormParameters from Query
Parameters, where the Servlet Spec doesn't. But is seems to me that it
makes JAX-RS and the Servlet Spec incompatible. Or am I missing
something that would make both work happily together?

Thanks,
Samuel Le Berrigaud.