RESTful Web Services with Spring Framework Part-5

Monil Joshi
3 min readOct 2, 2019

--

Please go through the below posts as this post is to be a continuation of those three parts. You can read RESTful Web Services with Spring Framework Part-1, Part-2, Part-3 and Part-4 here.

In this post, we will see how to validate the HTTP POST request body. We use a bean validation constraint to do so. As we have already added spring-boot-starter-web dependency in our maven, no need to add something else. In java code enable validation for beans and respective annotation. Add an annotation to createUser() before @RequestBody annotation called @Valid and import it. We can enable validation inside our bean i.e UserDetailsRequestModel class. We have to add a few annotations in the UserDetailsRequestModel class as well. A lot of annotations are listed here. In this post, we are using some of the annotations from the list

In UserDetailsRequestModel class. Add @NotNull annotation with the appropriate error message to the definition of variables that should not be null.

@NotNull(message=”First name can not be null”)
private String firstName;

If the value of any of these variables send in the HTTP POST request body is null (empty string)then we will get an error.

Calling client application which receives HTTP response will receive status code 400 bad request with a message describing reason of the error. We can provide customize error messages with message attributes in NotNull annotation. Use @Size annotation to limit character length with minimum and maximum limits.

Complete Code:

UserController.java
@PostMapping(consumes = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE},produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})public ResponseEntity<UserDetailsRequestModel> updateUser(@Valid @RequestBody UserDetailsRequestModel userDetailsRequestModel)
{
userDetailsRequestModel.setFirstName(userDetailsRequestModel.getFirstName());
userDetailsRequestModel.setLastName(userDetailsRequestModel.getLastName());userDetailsRequestModel.setEmailId(userDetailsRequestModel.getEmailId());userDetailsRequestModel.setUserId(userDetailsRequestModel.getUserId());userDetailsRequestModel.setPassword(userDetailsRequestModel.getPassword());return new ResponseEntity<UserDetailsRequestModel>(userDetailsRequestModel, HttpStatus.OK);
}
UserDetailsRequestModel.javapublic class UserDetailsRequestModel {@NotNull(message=”First name can not be null”)
private String firstName;
@NotNull(message=”Last name can not be null”)
private String lastName;
@NotNull(message=”emailId can not be null”)
@Email
private String emailId;
@NotNull(message=”password can not be null”)
@Size(min=8,max=16,message=”Password must be equal or greater that 8 characters and less than 16 characters”)
private String password;
@NotNull(message=”userId can not be null”)
private String userId;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmailId()
{
return emailId;
}
public void setEmailId(String emailId)
{
this.emailId = emailId;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUserId()
{
return userId;
}
public void setUserId(String userId)
{
this.userId = userId;
}
}

After this run the application. Go to the postman. Hit Http post request.

http://localhost:8080/users/

with payload body

{
"firstName":"John",
"lastName":"George",
"emailId":"john.george@gmail.com",
"userId":"JoGe",
"password":"abc"
}

Check the response status and response body.

Postman request and response

In the above screenshot, you can see that the request fails as the password is not full filling its required criteria. It is throwing status code 400, bad request with a message describing the reason for the error. An error message is the same which we sent in an annotation while defining the variable.

@Size(min=8,max=16,message=”Password must be equal or greater that 8 characters and less than 16 characters”)

You can try with different errors and appropriate error messages. Just send an incorrect email id (without domain) and check the response.

In the next post, we will see how to store the user's record temporary.

--

--

Monil Joshi

I am having an 8+ years of experience in software testing. Working with Web, Mobile and API technologies. Visit my GIT https://github.com/monilj