5

Can anyone please help me with this.

I created a web service using resteasy with wildfly 8.1.0 but @FormParam always returns null.

UserService.java

Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public class UserService {

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/create")
    public String create(@FormParam("first_name") String firstName, @FormParam("last_name") String lastName,
            @FormParam("email") String email, @FormParam("username") String username,
            @FormParam("password") String password, @FormParam("address") String address,
            @FormParam("country") String country, @FormParam("zip") String zip, @FormParam("ssnlast4") String ssnlast4,
            @FormParam("mobile") String mobile, @FormParam("dob_month") String dobMonth,
            @FormParam("dob_year") String dobYear, @FormParam("reg_type") String regType,
            @FormParam("avatar") String avatar) {

        String str = firstName  + ":" + lastName + ":" + email + ":" + username + ":" + password + ":" + address + ":" + country + ":" + zip+ ":" + ssnlast4 + ":" + mobile;
        return str;
    }
}

I use POSTMAN to test the service. enter image description here

return will be {null:null:null:null:null:null:null:null:null:null}

Thanks. I appreciate it.

blitzen12
  • 1,350
  • 3
  • 24
  • 33
  • Thanks. is it possible to support both form-data and `x-www-form-urlencoded`? i tried `@Consumes({"multipart/form-data","application/x-www-form-urlencoded"})` but it seems like `form-data` returns null while working in `www-form-urlencoded` – blitzen12 Jan 15 '15 at 06:38
  • It's not that simple. For multipart, you would need to grab the multipart dependency for resteas. And the annotations are different. Then you would need to check the actual Content-Type that comes in to determine how to proceed. You could alternatively have 2 methods that point to the same URI, just using different `@Consumes` that way one method will handle each content-type. See [here for multipart support](http://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html/Multipart.html) and [the dependency](http://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html/Maven_and_RESTEasy.html) – Paul Samsotha Jan 15 '15 at 06:49
  • 1
    Another thing also. You can use a Java object instead of all those fields. In the java class annotate the fields or setters with the `@FormParam`. And the just annotate parameter in the method with `@BeanParam`. That way your method won't be so cluttered – Paul Samsotha Jan 15 '15 at 08:07

3 Answers3

16

@FormParam is for application/x-www-form-urlencoded content type. The form-data button you selected in Postman will create mulitpart data. These are two different animals.

If you want your example to work, you should select the x-www-form-urlencoded button, then start adding key/value pairs. The key will be the @FormParam("key") that inject the value.

Also I would annotate your method with @Consumes(MediaType.APPLICATION_FORM_URLENCODED) so that any request with a content type that is not application/x-www-form-urlencoded will fail.

If you do want to use multipart data, then that's a different story. We can go down that path, if you confirm that is what you wanted. I don't see it being useful here though.

As an aside, you can click the Preview button and you will see the major difference in the body of how each type is sent.

Marko Zajc
  • 307
  • 3
  • 14
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
2

In our case the ContainerRequestFilter.filter(ContainerRequestContext) was calling containerRequestContext.getEntityStream() which seems to flush the buffer, and this caused the @FormParams to be null.

As a solution we send authentication token in header(ContainerRequestContext.getHeaders()) with the ajax request, which don't deal with the EntityStream.

4b0
  • 21,981
  • 30
  • 95
  • 142
  • 1
    I have the same problem right now and it's very annoying. There is a bug in the RESTEasy tracker (https://issues.jboss.org/browse/RESTEASY-567) but it got closed with no further work on the issue. Do you have a workaround for this? – Viciouss Sep 21 '16 at 08:54
  • 3
    I found a workaround. You just need to @Inject the HttpServletRequest and call the method getParameterMap() once before reading the stream via getEntityStream(). You don't need to do anything with it, just call the method. I'm definitely going to find out exactly why this is behaving as it is. – Viciouss Sep 21 '16 at 11:25
0

i will also face same problem because

@FormParam("name") String name

both places we should put same identifier otherwise it will give error.