I have a rest service in Jersey. I would like to have some post method that accepts parameters both in the form of multipart and in url encoding. I started with:
@POST @Path("/some/resource") public String addSomeResource(@FormParam("param") String param) { repository.add(new SomeResource(param)); }
My understanding is that using @Consumes more narrowly defines what is acceptable, and of course, this method is called if someone attaches form data in the usual way.
$.ajax({url:'/some/resource', type:'POST', data:'¶m=foo'});
or through a multi-page form submission (from some client http-client to transfer the client).
But when this happens through multipart, the mapping does not happen, and param
is null. I assume that this is because @FormDataParam
is expected in the method, not @FormParam
, but I also believe that I can only use @FormDataParam
when I more narrowly defined Requirements as multi-page. The documentation for FormDataParam
( http://jersey.java.net/nonav/apidocs/1.5/contribs/jersey-multipart/com/sun/jersey/multipart/FormDataParam.html ) seems to imply that FormDataParam
can return to FormData
if a multi-parameter parameter does not exist.
My current solution for this is to have two methods for each post parameter:
@POST @Path("/some/resource") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String addSomeResource(@FormParam("param") String param) { repository.add(new SomeResource(param)); } @POST @Path("/some/resource") @Consumes(MediaType.MULTIPART_FORM_DATA) public String addSomeResourceMP(@FormDataParam("param") String param) { return addSomeResource(param); }
It seems like this might work, but I wonder if I just misunderstand something more fundamental here or if there is no other fix that is slightly better.
Mikeb source share