@FormParam

I have been unifying the injection mechanism in Jersey, making it more modular and improving the performance of injection (now it is possible to plug-in your own injectable behavior for injecting on to fields, constructors and resource methods, including overriding or augmenting existing behavior... more later in another entry perhaps...).

As a serendipitous consequence of those changes it was so easy to implement the support for form parameters i could not resist (in fact the implementation is pluggable so i could have provided the functionality as a separate jar if so desired). In less than 2 hours it was in the main trunk :-)

So by popular request (many have requested this on the users list) I introduce @FormParam.

You can write a resource as follows:

@Path("/")
public class Resource {
  @POST
  @ConsumeMime(MediaType.APPLICATION_FORM_URLENCODED)
  public String post(
     @FormParam("a") String a,
     @FormParam("b") String b) {
   return a + b;
  }

and test it with the client API: 

WebResource r = Client.create().resource("/")'

Form form = new Form();
form.add("a", "foo");
form.add("b", "bar");

String s = r.post(String.class, form);

Form parameters will only work with resource methods where each parameter is annotated and at least one parameter is annotated with @FormParam. The same rules apply as for the other parameters, such as @QueryParam (with the exception that the encoded form is not currently supported). 

Comments:

What about supporting @FormParam in multipart/form-data forms?

Posted by Sergi on August 29, 2008 at 06:26 AM CEST #

There is support in Jersey. You can use any Java type that is supported by message body readers, such as JAXB beans. Just consume using "multipart/form-data".

The form parameter name is used to search for the body part with appropriate content disposition information.

This needs to be improved so that additional meta-data of the part can be obtained, for example:

@FormParam FormPart<MyJAXBType> p

Paul.

Posted by Paul Sandoz on August 29, 2008 at 07:52 AM CEST #

You're right.

I was trying @FormParam("appId") int applicationId
Changing it to @FormParam("appId") String applicationId solved it.

Is this the intended behaviour? I mean, in QueryParams, Strings are automatically translated into Integers if needed

Sergi

Posted by Sergi on September 03, 2008 at 03:58 AM CEST #

Oops, I've re-read it again. This is what you meant with FormPart, right? \^\^

Posted by Sergi on September 03, 2008 at 04:04 AM CEST #

FormPart is an idea that is not implemented, sorry for the confusion.

When using @FormParam for "multipart/form-data" it does not obey the same rules as when using it for MediaType.APPLICATION_FORM_URLENCODED, or the same rules for @QueryParam. Under those rules information is read from characters.

When using with "multipart/form-data" information is read from bytes using the content type of the body part and a matching MessageBodyReader for the annotated Java type.

Thus you can use JAXB beans or another other supported Java type that can be used directly for an entity.

There is no message body reader registered for a Java int type. What would be the media type for the serialization of an integer?

Posted by Paul Sandoz on September 03, 2008 at 04:56 AM CEST #

Got it.

>There is no message body reader registered for a Java int type. What would be the media type for the serialization of an integer?
Good point. application/octet-stream? Then it would be hard to differentiate between basic types.. Anyway I can live with some type checking on my web app :)

BTW, congratulations for your great work. You've made making applications that deal with forms ridiculously easy.

Posted by Sergi on September 03, 2008 at 05:45 AM CEST #

Hi Paul,

I'm a bit late considering the publish date of this article, but I just added a file upload to my rest service (using @FormParam("foo")File file) and I am wondering if it is possible to obtain the original file name inside my service method?

cheers,
Erik

Posted by Erik van Zijst on November 11, 2008 at 06:05 AM CET #

Hi Erik,

Your comment is well timed. I have recently added the class:

FormDataContentDisposition

and i think it should be possible to support:

@FormParam("name") FormDataContentDisposition d,
@FormParam("name") File f

and from "d" you get can get the filename property.

Posted by Paul Sandoz on November 11, 2008 at 06:35 AM CET #

I'm on 1.0 and couldn't find the class, but I now see that 1.0.1-SNAPSHOT is out already which has these things, so it's time to upgrade again :-)

cheers,
Erik

Posted by Erik van Zijst on November 11, 2008 at 07:04 AM CET #

I have just commited support in trunk as i described above. There should be new artifacts pushed to the 1.0.1-SNAPSHOT version in about 1 to 2 hours.

BTW a new version of the artifacts is pushed every time a commit to the trunk occurs.

Posted by Paul Sandoz on November 11, 2008 at 07:15 AM CET #

If anyone is using Jersey 1.x, you must use @FormParam(value="foo") instead of @FormParam("foo").

Posted by Kevin Bui on December 03, 2008 at 07:32 AM CET #

Hi Kevin,

The two annotation statements you provide are equivalent. I use the latter all the time without issue. For an annotation with one value it is not necessary to refer to the value name for assignment.

Are you having issues?

Paul.

Posted by Paul Sandoz on December 03, 2008 at 07:46 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

sandoz

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today