Skip to content
Advertisement

Springboot – validate @RequestBody

Question: It is possible to validate the JSON payload of a request body, without specifically writing if statements? Maybe via annotation or configuration?

I have a very easy POJO:

public class Foo {

    private int important;
    private String something;

//constructors, getter, seters, toString
}

And a very easy controller class:

@SpringBootApplication
@RestController
public class QuestionController {

    public static void main(String[] args) {
        SpringApplication.run(QuestionController.class, args);
    }

    @GetMapping(value = "/question")
    Mono<String> question(@RequestBody Foo foo) {
        System.out.println("The object foo, with value for important = " + foo.getImportant() + " and something = " + foo.getSomething());
        return Mono.just("question");
    }

}

If I query with a payload such as:

{
    "important": 42,
    "something": "value"
}

Everything is working perfectly fine, very happy.

However, if there is a typo: (note the typo on “important”)

{
    "importantWithTypo": 42,
    "something": "value"
}

Or the required “important” is absent (note the JSON is not even complete)

{
    "something": "value"
}

The request and computation are still valid! And the value of “important” is 0!

I do not want Spring to default to 0 and to thinks everything is fine.

I also do not want to change my types from primitives to boxed object.

Without me writing something like:

 @GetMapping(value = "/question")
    Mono<String> question(@RequestBody Foo foo) {
        if (0 == foo.getImportant()) {
            throw new IllegalArgumentException();
        }
        System.out.println("The object foo, with value for important = " + foo.getImportant() + " and something = " + foo.getSomething());
        return Mono.just("question");
    }

What is the most efficient way to resolve this? Some kind of annotation? Or maybe Spring boot configuration?

Thank you

Advertisement

Answer

Add @NotNull annotation on a field (you may need to change type to Integer), and add @Valid annotation on the method parameter of the controller.

Mono<String> question(@Valid @RequestBody Foo foo) {
    ...
}
public class Foo {

    @NotNull
    private Integer important;
    private String something;

//constructors, getter, seters, toString
}

You can find more information here: https://lmonkiewicz.medium.com/the-power-of-spring-rest-api-validation-77be83edef

Advertisement