0

I am using Spring Boot and the @NotNull is not working. When the name value is not provided the function runs fine.

@Entity
public class Employee  implements Serializable{
    @Id
    //@GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    //@Id
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

    @NotNull  // not working
    private String name;
    private String phone;
}

the controller is

@Controller
public class URLController {
    @Autowired
    EmployeeServiceImpl empService;

    @GetMapping({"/", "/index"})
    public String index1(Model model){
        model.addAttribute("employee",new Employee());
        return "index";
    }

    @PostMapping("/result")
    public String result(@Valid @ModelAttribute Employee employee){
        System.out.print(employee.getName()); 
        empService.save(employee);
        return "result"; 
    }
}

pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.springs</groupId>
<artifactId>springs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springs</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

i am using the model attribute to get the employee object. the pom.xml file contains the hibernate validator dependency.

dishank goyal
  • 105
  • 1
  • 4
  • 16
  • Can you post also the import zone of Employee class? –  Nov 04 '18 at 19:51
  • 2
    And tell precisely what function you're talking about – JB Nizet Nov 04 '18 at 19:53
  • have you added `@valid` or `@Validation` annotation while mapping model to request https://spring.io/guides/gs/validating-form-input/ – Ryuzaki L Nov 04 '18 at 19:53
  • import org.springframework.beans.factory.annotation.Required; import org.springframework.context.annotation.Bean; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.validation.constraints.NotNull; import java.io.Serializable; – dishank goyal Nov 04 '18 at 19:54
  • It depends on how the Employee instance is created. How are you using it ? – jordiburgos Nov 04 '18 at 20:06
  • Possible duplicate of [Spring boot starter data rest, @Notnull constraint not working](https://stackoverflow.com/questions/36649331/spring-boot-starter-data-rest-notnull-constraint-not-working) – Himanshu Nov 04 '18 at 20:33

2 Answers2

0

you must add @Valid annotaion in mapping part in your code for example

void addEmployee(@Valid Employee emp);

using this import "import javax.validation.Valid;"

Another note if you want to validate List you must make a wrapper list Like

public class ValidList<E> implements List<E> {

@Valid
private List<E> list;}

And override all methods in it

Mohamad Hatem
  • 53
  • 1
  • 7
0

Try modifying method like below to check details about errors.

@PostMapping("/result")
    public String result(@Valid @ModelAttribute("employee") Employee employee, BindingResult bindingResult){
        List<FieldError> errors = bindingResult.getFieldErrors();
        for (FieldError error : errors ) {
         System.out.println (error.getObjectName() + " - " +error.getDefaultMessage());
        }
        System.out.print(employee.getName()); 
        empService.save(employee);
        return "result"; 
    }

If no error in bindingResult then refer notnull-constraint-not-working to troubleshoot further.

Alien
  • 15,141
  • 6
  • 37
  • 57