M3d3L0
M3d3L0

Reputation: 45

JAX-RS (Jersey), Bean Validation, @JsonIgnore

I'm working on a sample project using :

Before adding the @JsonIgnore and @JsonProperty everything was working as expected, i was able to run my tests without a problem while performing bean validation on my object properties. Normally The "password" field should not be available for deserialization so i marked its getter with @JsonIgnore and its setter with @JsonProperty so as to allow serializing it when saving a new account object.

When launching my test i receive a 400 code error with following http response result :

avr. 26, 2016 12:25:29 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Client response received on thread main
1 < 400
1 < Connection: close
1 < Content-Length: 172
1 < Content-Type: application/json
1 < Date: Tue, 26 Apr 2016 11:25:29 GMT
1 < Vary: Accept
[{"message":"may not be null","messageTemplate":"{javax.validation.constraints.NotNull.message}","path":"AccountEndpoint.saveAccount.account.password","invalidValue":null}]

Note : When removing the @JsonIgnore annotation, no validation error is received of course

My Resource :

@Path("/account")
public class AccountEndpoint {

@POST
@Path("/save/")
@Produces(MediaType.APPLICATION_JSON)
public Response saveAccount(@Valid Account account){
        Account returned = accountService.saveAccount(account);
        return Response.status(200).entity(returned).build();

}

My Pojo Class :

public class Account implements Serializable {

private String username;
private String password;


// -- [username] ------------------------

@Size(max = 45)
@NotNull
@Column(name = "username", length = 45,unique=true)
public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@NotNull
@Size(max = 45)
@Column(name = "password", length = 45)
@JsonIgnore
public String getPassword() {
    return password;
}

@JsonProperty
public void setPassword(String password) {
    this.password = password;
    }
}

My Unit Test :

 public class AccountTest extends JerseyTest {

@Before
public void setUp() throws Exception {
    super.setUp();
}

@After
public void after() throws Exception {
    super.tearDown();
}

@Override
protected TestContainerFactory getTestContainerFactory() {
    return new GrizzlyWebTestContainerFactory();
}

@Override
protected DeploymentContext configureDeployment() {

    enable(TestProperties.LOG_TRAFFIC);
    enable(TestProperties.DUMP_ENTITY);

    return ServletDeploymentContext.forServlet(new ServletContainer(new
            JerseyConfig()))
            .contextParam("contextConfigLocation",TEST_APP_CONTEXT)
            .addListener(org.springframework.web.context.ContextLoaderListener.class)
            .servletPath("/").build();
}

@Test
public void testSave_New_User_Account {

    Account account = new Account();
    account.setUsername("Test");
    account.setPassword("Test");
    Response response =  target("/account/save").request().
            post(Entity.entity(account,MediaType.APPLICATION_JSON));

    assertEquals(200, response.getStatus());

}
}

JeseyConfig Class :

public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        packages("org.medel.endpoint");
    }
}

Upvotes: 2

Views: 1901

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208984

It's because on the client side the object is also being serialized by Jackson. And because the password property is ignored on reads, it never get serialized. So the request is being sent with no password. You might just want to create another test class for the entity, with no annotations, just for the client testing.

Edit

So just for clarity, the problem is with the client, not the server. When the client tries to serialize the Account, it doesn't serialize the password property, as it's ignored.

You can test it, but sending the JSON as a string, that way Jackson doesn't get involved. You will see that it works fine

.post(Entity.json("{\"username\":\"user\", \"password\":\"pass\"}"));

Upvotes: 2

Related Questions