user1354825
user1354825

Reputation: 1541

Spring RESTful application - POST method request body mandatory attributes

I am building a RESTful app in Spring Boot and i want to make few attributes in my POST method's request body mandatory.

In swagger yaml, i mark them as required "true", but when i generate the classes using swagger editor, i dont see that impacting in any way, i.e i can't see even a @NotNull annotation or anything of that sort.

How do i mark them as mandatory in my java model class ? Is @NotNull the way to go?

If yes, should i do that in my request body class, or in the jpa document class or both ?

Thanks !

Upvotes: 1

Views: 744

Answers (1)

Łukasz Olszewski
Łukasz Olszewski

Reputation: 915

Yes, @NotNull is a way to go.

But also You need to use @Valid annotation.

check example:

@RequestMapping(value = "/appointments", method = RequestMethod.POST)
    public String add(@Valid AppointmentForm form, BindingResult result) {
        ....
    }

    static class AppointmentForm {

        @NotNull
        private Date date;
    }

Upvotes: 1

Related Questions