Hi,
I am trying to create a controller endpoint inside my custom module, so I am trying to use @valid @responsebody for receiving body of POST with validation and serialize them into my model with the help of constraints available in javax and hibernate validator , but during runtime I am not able to get any validation error in case constraints fail when supplied the incorrect key-value in request body. Its just serializing the body into the model with null
value for those keys not provided in the request.
Runtime details:
- openmrs-2.1.1.
- validation-api-1.0.0.GA.jar
- hibernate-validator-4.2.0.Final.jar
Model.java
package org.custommodule.requestmodels;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Email;
import javax.validation.Valid;
import java.util.*;
public class SignupRequest {
public static class UserAttributes {
@NotEmpty(message = "'type' missing in userAttributes")
private String type;
@NotEmpty(message = "'value' missing in userAttributes")
private String value;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@NotEmpty(message = "'firstName' missing")
private String firstName;
@NotEmpty(message = "'lastName' missing")
private String lastName;
@Email(message = "Not a valid E-mail")
private String email;
@NotEmpty(message = "'isProvider' missing")
private Boolean isProvider;
private String password;
@NotEmpty(message = "'type' missing")
private String type;
@NotEmpty(message = "'gender' missing")
private String gender;
@NotEmpty(message = "'phone' missing")
private String phone;
@NotEmpty(message = "'country' missing")
private String country;
@NotEmpty(message = "'dob' missing")
private String dob;
@Valid
private ArrayList<UserAttributes> userAttributes;
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Boolean getIsProvider() {
return isProvider;
}
public void setIsProvider(Boolean isProvider) {
this.isProvider = isProvider;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public ArrayList<UserAttributes> getUserAttributes() {
return userAttributes;
}
public void setUserAttributes(ArrayList<UserAttributes> userAttributes) {
this.userAttributes = userAttributes;
}
}
Controller.java
@RequestMapping(value = "/v5", method = RequestMethod.POST)
public @ResponseBody
Object createNewUser(@Valid @RequestBody SignupRequest signupRequest,HttpServletRequest request,
HttpServletResponse response) throws ResponseException {
SimpleObject result = new SimpleObject();
result.put("req", signupRequest);
return result;
}
Can anyone point what i am doing wrong or missing something.
Thanks