I'm working on a sample project using :
- JAX-RS (Jersey 2)
- JSR-303 Bean validaiton
- Jersey Test for testing
- Grizzly Http container
- Jackson 2.7.3
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");
}
}