title: HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `int` from String
body: <p><strong>How to show conversion error along with other validation errors when creating REST API?</strong></p>
  <ul>
  <li>Spring Boot</li>
  <li>Spring MVC</li>
  <li>REST Controller</li>
  </ul>
  <pre><code>@Data
  @AllArgsConstructor
  class DTO
  {
      @NotNull
      @Min(1)
      private Integer id;
  }
  
  @RestController
  class ExampleController
  {
      @PostMapping(&quot;/&quot;)
      public void createEndpoint(@Valid @RequestBody DTO dto) {
          return &quot;{\&quot;message\&quot;: \&quot;OK\&quot;}&quot;;
      }
  }
  </code></pre>
  <p>When I make request to this endpoint,</p>
  <pre><code>POST /
  {
      &quot;id: &quot;abrakadabra&quot;
  }
  </code></pre>
  <p>I would like to get something like this</p>
  <pre><code>{
      &quot;errors&quot;: {
          &quot;id&quot;: [
              { code: &quot;invalid_format&quot;, message: &quot;Field must contain only digits&quot; }
          ]
      }
  }
  </code></pre>
  <p>What I actually get?</p>
  <pre><code>Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `int` from String &quot;abrakadabra&quot;: not a valid `int` value;
  nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String &quot;abrakadabra&quot;: not a valid `int` value
  at [Source: (PushbackInputStream); line: 2, column: 24] (through reference chain: com.mylid.back.dtos.CreateCompanyDto[&quot;pricing_plan_id&quot;])]
  </code></pre>
  <p>I know that I can create custom annotation for validation.
  BUT the problem is that the process does not reach validation. It fails on deserialization step.</p>
  <p>[Possible way]</p>
  <p><em>There is one dirty trick, to change Integer type in DTO to String. Create custom annotation to check if String contains only digits. Then manually map this String from DTO to Integer in Entity. Then save to database.</em></p>
  <p>What are other ways how to solve this problem?
  It is very weird, that there is a few topics on Google and StackOverFlow for this particular problem. And on almost every page the accepted answer is &quot;it is not our problem, API clients should pass integer&quot;.</p>
  <p>In PHP we can easily do it with 'integer' validator, almost every framework has it, and it will not prevent other fields from validation if I pass &quot;abracadabra&quot; instead of &quot;1&quot;.</p>