Hi,
I've got a REST service as below -
@POST
@Path("/createPdcUser")
@Produces("application/json")
@Consumes("application/json")
public Response createUser(CreateUser user) {
CreateUserResponse response = getAnonymousServiceManager().createPdcUser(user);
if (response.hasErrors()) {
return Response.status(Status.NOT_MODIFIED).entity(response).build()
} else {
return Response.status(Status.CREATED).entity(response).build()
}
}
The service must return a 201 CREATED http header on successfully creating a
user.
On all validation errors, such as duplicate username exists, the service
returns
a 304 NOT_MODIFIED.
Is this the correct approach to REST?
A test then attempts to create a user via this REST service, it is defined
below
as -
import org.apache.cxf.jaxrs.client.WebClient;
public void testCreatePdcUserEmailsDoNotMatch() throws Exception {
CreateUser user = new CreateUser();
user.setEmail(email);
user.setConfirmEmail(StringUtils.isEmpty(confirmEmail) ? email : confirmEmail);
user.setPassword("password1x@xcom");
user.setConfirmPassword("password1x@xcom");
user.setUsername("username");
CreateUserResponse response = WebClient.create(USER_SERVICE_URL)
.path("/createUser")
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.post(pdcUser, CreateUserResponse.class);
assertNull(response.getUserId());
assertEquals("The email address entered does not match. Please try again.",
response.getErrors().get("confirmEmailAddress"));
}
However, when the test above is run, on a successful response (201), the call
to
WebClient.create returns the 'CreateUserResponse' object as expected.
On an unsuccessful response (304), the same call returns a null in
the 'CreateUserResponse' object, even though the webservice includes a response.
1) is this the correct approach to writing a POST consuming REST service?
2) how can i extract the CreateUserResponse object when the REST service
returns
a 304?
Thanks,
Gavin