Suppose I have a class, what is the order of validation in a SpringBoot class object. After an object gets created then the fields are populated or does the validation happens before the objects are populated, at the time of setting of the field values this validation happens. Or after the object is created then by a get call we validate the object field values.
package com.bablo.google.request;
import java.io.Serializable;
import java.util.Set;
import javax.validation.constraints.NotNull;
public class SomeRequest implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#NotNull
private Long userId;
private String resNote; //this is not annotated with #NotNull
#NotNull
private String revTag;
public Long getUserId() {
return userId;
}
public void setUserId(final Long userId) {
this.userId = userId;
}
public String getResNote() {
return responseNote;
}
public void setResNote(final String resNote) {
this.resNote = resNote.trim(); //Call to trim() method of the String object.
}
public String getRevTag() {
return revTag;
}
public void setRevTag(final String revTag) {
this.revTag = revTag.trim(); //Call to the trim() method of the String object.
}
}
What is the way that validation will happen in a class? What is the mechanism of validating the fields, does the #NotNull validation or for that matter any validation depends on the getter methods to do the validation?
Do they first call the setter methods to do the validation?
Splitting your questions and adding answers.
What is the order of validation in a SpringBoot class object?
Validation happens as part of data binding process. Every request parameter/path variable will be validated as per the marked annotation and only when the validation passes, the value will be assigned to the class object.
What is the way that validation will happen in a class?
Validation process differs for each binding mechanism. If the method parameter is ModelAttribute/request parameter/path variable/Map etc. Spring uses different argument resolvers for each method parameter. If #Valid is added, then it enables validation during argument resolution process (Look out for RequestMappingHandlerAdapter where the whole magic is wired).
Does the #NotNull validation or for that matter any validation depends on the getter methods to do the validation? Do they first call the setter methods to do the validation?
Spring uses reflection to construct/validate the method argument class. Data binding and validation happens even without getters/setters.
You can validate manually by calling
#Autowired
private javax.validation.Validator validator;
...
validator.validate(new SomeRequest()); // you can also pass method's argument
or you can use auto validation
Here is an example https://www.baeldung.com/spring-boot-bean-validation of using #Valid + #ExceptionHandler
Here is an example https://spring.io/guides/gs/validating-form-input/ of using #Valid + BindingResult
Related
I want to use javax validation on poco objects that contain complex types. In my code, I want to validate the PersonDetail object inside my Person class. If I don't use the #Valid PersonDetail, then validations on that subclass don't work.
Is there any way to validate nested objects without the #Valid annotation on each one?
public class Person {
#Pattern(regexp = "^[a-zA-Z]+$")
private String surname;
#Valid(//without this personDetails validations not worked)
private PersonDetail personDetail;
....
PersonDetail class
public class PersonDetail {
#Pattern(regexp = "^[a-zA-Z]+$")
private String surname2;
public String getSurname2() {
return surname2;
}
No, you need #Valid on the personDetail field in order for validation to continue to look down into that field. You can configure this in other ways (validation.xml), but ultimately you need to tell the Validator to descend into the value of the personDetail field.
I have a configuration class like below. All of fields in the inner class OptionalServiceConfigs has a default value as annotated using #Value as shown in below.
Sometimes in my application.properties file, it does not have a single service prefixed property. In that case, we want to have loaded an OptionalServiceConfigs instance with its default field values.
#Configuration
#ConfigurationProperties(prefix = "myconf")
public class MyConfigs {
// ... rest of my configs
#Value("${service:?????}") // what to put here, or can I?
private OptionalServiceConfigs service; // this is null
// In this class all fields have a default value.
public static class OptionalServiceConfigs {
#Value("${mode:local}")
private String mode;
#Value("${timeout:30000}")
private long timeout;
// ... rest of getter and setters
}
// ... rest of getter and setters
}
But unfortunately, the service field is null when it is accessed using its getter method. Because spring boot does not initialize an instance of it when there is no property keys found with prefixed myconf.service.* in my application.properties file.
Question:
How can I make service field to initialize to a new instance along with its specified default field values when there are no corresponding prefixed keys in properties file?
I can't imagine a value to put in annotation #Value("${service:?????}") for service field.
Nothing works, tried, #Value("${service:}") or #Value("${service:new")
Based on #M. Deinum's advice, did some changes to configuration class. I am a newbie to Spring and it seems I have misunderstood how Spring works behind-the-scenes.
First I removed all #Value annotation from inner class (i.e. OptionalServiceConfigs), and as well as service field in MyConfigs class.
Then, initialized all inner class fields with their default values inline.
In the constructor of MyConfigs, I initialized a new instance of OptionalServiceConfigs for the field service.
By doing this, whenever there is no service related keys in my application.properties a new instance has already been created with default values.
When there is/are service related key/s, then Spring does override my default values to the specified values in application.properties only the field(s) I've specified.
I believe from Spring perspective that there is no way it can know in advance that a referencing field (i.e. service field) would be related to the configurations, when none of its keys exist in the configuration file. That must be the reason why Spring does not initialize it. Fair enough.
Complete solution:
#Configuration
#ConfigurationProperties(prefix = "myconf")
public class MyConfigs {
// ... rest of my configs
private OptionalServiceConfigs service;
public static class OptionalServiceConfigs {
private String mode = "local";
private long timeout = 30000L;
// ... rest of getter and setters
}
public MyConfigs() {
service = new OptionalServiceConfigs();
}
// ... rest of getter and setters
}
you can try such a structure which works for me quite fine:
#Data
#Validated
#ConfigurationProperties(prefix = "gateway.auth")
#Configuration
public class AuthProperties {
#NotNull
private URL apiUrl;
#Valid
#NotNull
private Authentication authentication;
#Data
public static class Authentication {
#NotNull
private Duration accessTokenTtl;
#NotNull
private String accessTokenUri;
#NotNull
private String clientId;
#NotNull
private String clientSecret;
#NotNull
private String username;
#NotNull
private String password;
#Min(0)
#NonNull
private Integer retries = 0;
}
}
Important is to have getters and setters in order to enable Spring to postprocess ConfigurationProperties, I am using Lombok (#Data) for this.
please see here for more details:
Baeldung ConfigurationProperties Tutorial
I have a simple method to get a list of documents for a given companyId. Here is the method:
#Override
public List<Documents> getDocumentList(#NotNull Integer companyId) {
Company company = new Company(companyId);
return this.documentRepository.findByCompany(company);
}
I wanted to use Javax validation constraints to ensure that the companyId being passed in, is not null. But it seems to not have any effect, as I'm able to pass in a null value, and it flows down to the findByCompany call on the repository. I also added #Valid before #NotNull to force validation, but that too didn't do anything.
I could always write a couple of lines to check for a null value, but wanted to use javax.validation annotations to make the code more readable and concise. Is there a way to make the annotations work on method params?
To activate parameter validation, simply annotate the class with #Validated
import org.springframework.validation.annotation.Validated;
From The Java EE 6 Tutorial:
The Bean Validation model is supported by constraints in the form of
annotations placed on a field, method, or class of a JavaBeans
component, such as a managed bean.
You should place your validation of a field related to a declared bean, something like this:
#Entity
#Table(name="users")
public class BackgammonUser {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long userId;
#Column(name="username")
#NotBlank
private String userName;
#NotBlank
private String password;
#NotNull
private Boolean enabled;
}
The BackgammonUser is considered to be a bean.
If you #Inject a class with your method, its working as expected.
#Stateless
public class MyBean{
#Inject
TestClass test;
}
and
public class TestClass {
public List<Documents> getDocumentList(#NotNull Integer companyId)
{
//...
}
}
ConstraintViolationException when you call your method with null parameter:
WFLYEJB0034: EJB Invocation failed on component MyBean for method ...:
javax.ejb.EJBException: javax.validation.ConstraintViolationException:
1 constraint violation(s) occurred during method validation.
#NotNull Annotation,
A method should not return null.
A variable (like fields, local variables, and parameters) cannot hold null value.
Let's say I have a class that has 3 members:
Class A {
private String string1; /** Cannot be null */
private String string2; /** Cannot be null */
private String string3; /** Can be null */
}
I have 2 method that accepts an object of this class as a parameter. One of the methods needs to check that the non nullable fields are present while in the other one, it doesn't matter:
public int func1(A object); /** Check non nullable fields */
public int func2(A object); /** No check required */
Is there any clean way to do it? Using #NonNull annotations or something?
I have tried various ways but none of them work. All the NonNull only help make sure that the setter doesn't get null as the parameter. Or that the object itself isn't null.
I can't seem to find something that does this kind of recursive null check on the object.
It'd be great if any of you could help. :)
You need a bean Validator, a class used to check that the bean is OK. In Spring there are a number of implementations. For example, see SmartValidatorand LocalValidatorFactoryBean
The #valid annotation is a nice way to call automagically the validator. As you are using Spring you can avoid the manual creation of the validator. It only works if the method is called by Spring (or any equivalent container). You may get the validation results in a BindingResult object. For example:
#RequestMapping(value = "/MyPath", method = RequestMethod.POST)
public String postCostForm(Model model, #Valid MyForm myForm, BindingResult result){
if(result.hasErrors()){
for(ObjectError error : result.getAllErrors()){
// Do something
}
return "/error";
}else{
return "/success";
}
}
Validation is very powerfull and sometimes complex. You may create groups of validation and check just one group of them. You can create your custom constraint tags, you can call validation methods and you may customize and internationalize the messages returned if the validation fails.
class A {
#NotNull
private String string1; /** Cannot be null */
#NotNull
private String string2; /** Cannot be null */
private String string3; /** Can be null */
}
And in the method signature have #Valid
public int function(#Valid A object)
Use #Required annotation in Spring's
private String string1;
#Required
public void setString1(String string1) {
this.string1= string1;
}
I worked out a concept to conditionally validate using JSR 303 groups. "Conditionally" means that I have some fields which are only relevant if another field has a specific value.
Example: There is an option to select whether to register as a person or as a company. When selecting company, the user has to fill a field containing the name of the company.
Now I thought I use groups for that:
class RegisterForm
{
public interface BasicCheck {}
public interface UserCheck {}
public interface CompanyCheck {}
#NotNull(groups = BasicCheck.class)
private Boolean isCompany
#NotNull(groups = UserCheck.class)
private String firstName;
#NotNull(groups = UserCheck.class)
private String lastName;
#NotNull(groups = CompanyCheck.class)
private String companyName;
// getters / setters ...
}
In my controller, I validate step by step depending on the respective selection:
#Autowired
SmartValidator validator;
public void onRequest(#ModelAttribute("registerForm") RegisterForm registerForm, BindingResult result)
{
validator.validate(registerForm, result, RegisterForm.BasicCheck.class);
if (result.hasErrors()
return;
// basic check successful => we can process fields which are covered by this check
if (registerForm.getIsCompany())
{
validator.validate(registerForm, result, RegisterForm.CompanyCheck.class)
}
else
{
validator.validate(registerForm, result, RegisterForm.UserCheck.class);
}
if (!result.hasErrors())
{
// process registration
}
}
I only want to validate what must be validated. If the user selects "company" fills a field with invalid content and then switches back to "user", the invalid company related content must be ignored by the validator. A solution would be to clear those fields using Javascript, but I also want my forms to work with javascript disabled. This is why I totally like the approach shown above.
But Spring breaks this idea due to data binding. Before validation starts, Spring binds the data to registerForm. It adds error to result if, for instance, types are incompatible (expected int-value, but user filled the form with letters). This is a problem as these errors are shown in the JSP-view by <form:errors /> tags
Now I found a way to prevent Spring from adding those errors to the binding result by implementing a custom BindingErrorProcessor. If a field contains null I know that there was a validation error. In my concept null is not allowed - every field gets annotated with #NotNull plus the respective validation group.
As I am new to Spring and JSR-303 I wonder, whether I am totally on the wrong path. The fact that I have to implement a couple of things on my own makes me uncertain. Is this a clean solution? Is there a better solution for the same problem, as I think this is a common problem?
EDIT
Please see my answer here if you are interested in my solution in detail: https://stackoverflow.com/a/30500985/395879
You are correct that Spring MVC is a bit picky in this regard,and it is a common problem. But there are work-arounds:
Make all your backing fields strings, and do number/date etc conversions and null checks manually.
Use JavaScript to set fields to null when they become irrelevant.
Use JavaScript to validate fields when they are entered. This will fix almost all of your problems.
Good luck!
I know this question is old, but I came upon it looking for an answer for a different situation.
I think for your situation you could use inheritance for the forms and then use two controller methods:
The forms would look like this:
public class RegistrationForm
{
// Common fields go here.
}
public class UserRegistrationForm
extends RegistrationForm
{
#NotNull
private String firstName;
#NotNull
private String lastName;
// getters / setters ...
}
public class CompanyRegistrationForm
extends RegistrationForm
{
#NotNull
private String companyName;
// getters / setters ...
}
The controller methods would look like this:
#RequestMapping(method = RequestMethod.POST, params = "isCompany=false")
public void onRequest(
#ModelAttribute("registerForm") #Valid UserRegistrationForm form,
BindingResult result)
{
if (!result.hasErrors())
{
// process registration
}
}
#RequestMapping(method = RequestMethod.POST, params = "isCompany=true")
public void onRequest(
#ModelAttribute("registerForm") #Valid CompanyRegistrationForm form,
BindingResult result)
{
if (!result.hasErrors())
{
// process registration
}
}
Notice that the #RequestMapping annotations include a params attribute so the value of the isCompany parameter determines which method is called.
Also notice that the #Valid annotation is place on the form parameter.
Finally, no groups are needed in this case.