0

I'm using spring boot to build API, in post request I use Valid notation to validate user input that is posted as JSON file, however when I test it by leaving some fields empty they are still passed. I'm not sure why since I'm using valid notation before the object argument.

what can be wrong?

Object class


    @NotNull(message = "Please provide a name")
    private String name;

    @NotNull(message = "Please provide a gender")
    private Gender gender;

    @NotNull(message = "Please provide a birthDay")
    private String birthDay;

    @NotNull(message = "Please provide a latitude")
    private double latitude;

    @NotNull(message = "Please provide a longitude")
    private double longitude;

    public Wolf(String id, @NotNull String name, @NotNull Gender gender, @NotNull String birthDay, @NotNull double latitude, @NotNull double longitude)
    {
        this.id = id!=null ? id : UUID.randomUUID().toString();
        this.name= name;
        this.gender= gender;
        this.birthDay= birthDay;
        this.latitude= latitude;
        this.longitude= longitude;
    }

rest-controller class

@Autowired
    private Wolf_Service wolf_service;

    @RequestMapping("Wolf/wolf_list")
    public List<Wolf> All_wolfs()
    {
        return wolf_service.display_wolf_list();
    }
  @PostMapping(value = "Wolf/createwolf", consumes = "application/json", produces = "application/json")
    public Wolf createwolf (@Valid @RequestBody Wolf wolf)   {
        
        var isAdded =  wolf_service.add_wolf(wolf);
        if (!isAdded) {
            return null;
        }
            return wolf;
    }

4 Answers 4

2

This issue often comes in latest version of spring boot(2.3.0).

You would need to add the below dependency:

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

Note : You can use hibernate-validator instead of spring-boot-starter-validation.

1
  • This fixed my problem, Thanks a lot
    – user11338912
    Commented Oct 27, 2020 at 20:58
1

Check that you import correctly from Validation.Constraints not from com.sun.istack.

import javax.validation.constraints.NotNull;

If that not fix your problem. Check your pom.xml, if you have used validation dependency.

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

It depends on how you are handling your null values. If i.e. leaving a String field empty means you will send "" this will be passed as it is not a null value, hence the @NotNull annotation on your Wolf class does not apply.

Michael

0

Make sure you import all the import is from

jakarta.validation.Valid

As validation constraints of "spring boot starter validation api" does not work you need to replace this with the jakarta one. Gradle lib for reference given below

implementation 'jakarta.validation:jakarta.validation-api:3.0.2'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.