Anup Deshpande
Anup Deshpande

Reputation: 181

How to validate input model using spring validation for ReST web services & return appropriate error message

my task is to validate input parameters send through ReST - Client, before processing the input parameters, the validation containts to check NotNull, Email & number inputs for specific fields, for that i'm trying to use some annotations but not working but if input is passed wrong showing bad request instead of returning error message

my input module is as

@Id
@GeneratedValue
@Column(name = "user_id")
private Integer user_id;
@Column(name = "user_email_id")
@NotEmpty(message = "Please enter your Email_Id.")
@Email
private String user_email_id;
@NotEmpty(message = "Please enter your Password.")
@Column(name = "user_password")
private String user_password;
@NotEmpty(message = "Please enter your FirstName.")
@Column(name = "firstname")
private String firstname;
@NotEmpty(message = "Please enter your LastName.")
@Column(name = "lastname")
private String lastname;
@Column(name = "mobile_number")
private String mobile_number;
@Column(name = "user_status")
private Integer user_status;
@Column(name = "isdeleted")
private Integer isdeleted;
@Column(name = "created_by")
private Integer created_by;
@Column(name = "profile_picturename")
private String profile_picturename;
@Column(name = "address")
private String address;

method for same is as

@RequestMapping(value = "/validate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserInputModel> AddUser(
        @Valid @RequestBody UserInputModel userInputModel) throws Exception     {

return new ResponseEntity<UserInputModel>(userInputModel, HttpStatus.OK);   
}

& passing parameters are as

{
  "user_id":1,
  "user_email_id": "sdfdfgfgf",
  "user_password": "fdsdsdf",
  "firstname": "sdfsdf",
  "lastname": "sfdsdfds",
  "mobile_number": "sdfgdgdf",
  "user_status": 1,
  "isdeleted": 0,
  "created_by": 1,
  "profile_picturename": "kfksdjfhksjd",
  "address": "sfdsdfsd"
}

I'm getting "HTTP/1.1 400 Bad Request" Exception,if i pass parameters either empty or not in proper format, How can i return appropriate message

Thanks in advance

Upvotes: 2

Views: 917

Answers (1)

Anup Deshpande
Anup Deshpande

Reputation: 181

got the answer, this post might help somebudy else as well who might have faced same problem

Spring MVC - REST Webservices. Bean Validation Messages

Upvotes: 2

Related Questions