title: Springboot - validate @RequestBody
body: <p>Question: It is possible to validate the JSON payload of a request body, without specifically writing if statements? Maybe via annotation or configuration?</p>
  <p>I have a very easy POJO:</p>
  <pre><code>public class Foo {
  
      private int important;
      private String something;
  
  //constructors, getter, seters, toString
  }
  </code></pre>
  <p>And a very easy controller class:</p>
  <pre><code>@SpringBootApplication
  @RestController
  public class QuestionController {
  
      public static void main(String[] args) {
          SpringApplication.run(QuestionController.class, args);
      }
  
      @GetMapping(value = &quot;/question&quot;)
      Mono&lt;String&gt; question(@RequestBody Foo foo) {
          System.out.println(&quot;The object foo, with value for important = &quot; + foo.getImportant() + &quot; and something = &quot; + foo.getSomething());
          return Mono.just(&quot;question&quot;);
      }
  
  }
  </code></pre>
  <p>If I query with a payload such as:</p>
  <pre><code>{
      &quot;important&quot;: 42,
      &quot;something&quot;: &quot;value&quot;
  }
  </code></pre>
  <p>Everything is working perfectly fine, very happy.</p>
  <p>However, if there is a typo: (note the typo on &quot;important&quot;)</p>
  <pre><code>{
      &quot;importantWithTypo&quot;: 42,
      &quot;something&quot;: &quot;value&quot;
  }
  </code></pre>
  <p>Or the required &quot;important&quot; is absent (note the JSON is not even complete)</p>
  <pre><code>{
      &quot;something&quot;: &quot;value&quot;
  }
  </code></pre>
  <p>The request and computation are still valid! And the value of &quot;important&quot; is 0!</p>
  <p>I do not want Spring to default to 0 and to thinks everything is fine.</p>
  <p>I also do not want to change my types from primitives to boxed object.</p>
  <p>Without me writing something like:</p>
  <pre><code> @GetMapping(value = &quot;/question&quot;)
      Mono&lt;String&gt; question(@RequestBody Foo foo) {
          if (0 == foo.getImportant()) {
              throw new IllegalArgumentException();
          }
          System.out.println(&quot;The object foo, with value for important = &quot; + foo.getImportant() + &quot; and something = &quot; + foo.getSomething());
          return Mono.just(&quot;question&quot;);
      }
  </code></pre>
  <p>What is the most efficient way to resolve this? Some kind of annotation? Or maybe Spring boot configuration?</p>
  <p>Thank you</p>