SpringBoot: How do I validate the fields of an argument's field in SpringBoot?

Viewed 42

I'm trying to validate the credit card's information when a user inputs it.

Not sure what is going wrong. This is what I've tried so far, but it's giving me the message that "Validation failed for classes (CreditCard) during persist time for groups.

@RequestController
@RequestMapping
public class fooController {

...

    @PostMapping
    public ResponseEntity saveFoo(@Valid @RequestBody Sale sale) {
        fooService.saveFoo(sale);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
public class Sale {
    @Id
    private Long id;
    
    @Valid
    private CreditCard creditCard
@Embeddable
public class CreditCard {

@NotBlank
private String cardNumber;

@NotBlank
private String cvv;

}
0 Answers
Related