Moiz Raja
Moiz Raja

Reputation: 5760

Bean input not being populated by jax-rs

I have the following jax-rs resource,

@Path("admin/user")
@Api(value = "Administration - User")
@Produces(MediaType.APPLICATION_JSON)
public class UserResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void createUser(@BeanParam User user) {
       // ....
    }
}

My User bean is defined as follows,

public class User {
    @FormParam("firstName")
    private String firstName;

    @FormParam("lastName")
    private String lastName;
}

When I make a REST request to create a User with the request body being a json the User object does get created but the fields are not populated. I was able to verify that the request body indeed contains the json passed.

I am using wildfly 8.2.0.

My maven dependencies include,

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.7.4</version>
    </dependency>

and

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>3.0.10.Final</version>
        <scope>provided</scope>
    </dependency>

What am I missing?

Edit - 1

When I manually read the json body from the servlet request and run it through the Object mapper it seems to work just fine,

    ObjectMapper objectMapper = new ObjectMapper();

    final User user = objectMapper.readValue(json, User.class);

Upvotes: 1

Views: 263

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209012

Get rid of the @BeanParam and @FormParam. These are not used for JSON bodies. And annotation-less parameter is enough to make it work.

Upvotes: 2

Related Questions