'Validation is not working in Spring boot for the rest requests

Hi i have been trying to validate the data of the rest request through "JSR-303" and the code are as follows -:

*pojo annotations level

@NotNull(message="Carrier ID cannot be null")
    private String carrier;

*MessageStatusDoc class

@Id
private String transactionId;

private String status;

private Key key;

private AccountDetail accountDetail;

*calling method

saveMessage(@Valid @RequestBody MessageStatusDoc messageStatusDoc)

The key Class contatins the carrier field

Can any one suggest why i am not able to catch the error..?

PS the value is printed as null on the console when i am trying to log it ..



Solution 1:[1]

The code you provided seems fine. Altough you did not provide your controller annotations. E.g.

@PostMapping
public void saveMessage(@Valid @RequestBody MessageStatusDoc messageStatusDoc)

However be sure to add

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

to your pom file (at least for Spring Boot >= 2.3.0). This will include the validation api along with the hibernate validator which does the actual validation work.

Furthermore if having the annotations in an external package try not to mix

    <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
    </dependency>

and

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>

Hope this helps!

Solution 2:[2]

Here is the example.

I assume below class is your MessageStatusDoc.

The carrier feild is your need to validate.

public class MessageStatusDoc {

        @Id
        private String transactionId;

        private String status;

        private Key key;

        @NotNull(message="Carrier ID cannot be null")
        private String carrier;

        //getter setter
}

Here is the method which use MessageStatusDoc as a parameter

public void saveMessage(@Valid @RequestBody MessageStatusDoc messageStatusDoc) {
   //nothing to do for now
}

If you do it well like given example then if any method call the saveMessage method but the carrier field of the messageStatusDoc parameter is null then the validation will throw an exception.

And the end. I have a question. Can you please give the all code ?

Solution 3:[3]

This will be resolved if you use the annotations at the getter level not at the fields declaration level.

Solution 4:[4]

I had the same problem after adding all starter file. But restarting my eclipse solved the problem.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Georg Moser
Solution 2
Solution 3 Rishabh Bansal
Solution 4 Vikas s kumar