D
D
dm_black2015-12-16 19:05:36
Java
dm_black, 2015-12-16 19:05:36

Issues with validation on Spring Boot + Oval?

Hello I am using Spring Boot 1.3 and OVal 1.85. My Model:

@Entity
@Table(name = "company")
public class Company extends BaseModel {

    @NotBlank
    @NotNull
    @Length(min = 5, max = 50)
    @Column(nullable = false, name = "name", length = 50)
    private String name;

}

My controller:
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @RequestMapping(value = "", method = RequestMethod.POST)
    public Company saveCompany(@RequestBody(required = true) @Valid Company company) {
        return companyService.save(company);
    }
}

But the valid annotation skips invalid values. Project without XML, only annotations. What can you advise?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2015-12-17
@zolt85

I can advise you to write your own validator. A validator is a class that implements the org.springframework.validation.Validator interface. Then this validator must be added in such a not tricky way in the controller:

/** */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.addValidators(new CompanyValidator());
    }

and in saveCompany use @Validated annotation instead of valid

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question