Jay
Jay

Reputation: 5074

Method parameter annotated with @FormParam is always null in JAX-RS

I am trying to send multiple form parameters to my REST servive using POST. But the parameters sent by the client are always received as null.

@POST
@Path("/login")
@Produces({ "application/json" })
public LoginData userLogin(@FormParam("picture") String picture, 
                           @FormParam("name") String name, 
                           @FormParam("email") String email) {
    ...
}

When I remove all the parameters like the code below, it works properly:

@POST
@Path("/login")
@Produces({ "application/json" })
public LoginData userLogin() {
    ...
}

I've checked and the values sent by the client are not null.

Is there a different way to receive the parameters?

Upvotes: 0

Views: 2384

Answers (2)

vsriram92
vsriram92

Reputation: 663

This might not be useful for the actual question. Might be useful for some people who has similar issue along with consumes annotation.

I had the similar issue even if I have the annotation it was null. The reason was like my project was using both org.codehaus.jackson library and com.fasterxml.jackson libraries. So, mapping had issues.

Once I updated the parent and related child projects to fasterxml, the formparam annotation was working fine.

So search your project and update all the references in pom.xml and java imports from org.codehaus to com.fasterxml. FormParam and HeaderParam null issue will be resolved.

Upvotes: 0

cassiomolin
cassiomolin

Reputation: 130857

Annotate your method with @Consumes(MediaType.APPLICATION_FORM_URLENCODED):

@POST
@Path("/login")
@Produces(MediaType.JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public LoginData userLogin(@FormParam("picture") String picture, 
                           @FormParam("name") String name, 
                           @FormParam("email") String email) {

    ...
}

And ensure the Content-Type of the request is application/x-www-form-urlencoded.

Upvotes: 1

Related Questions