Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How to bind Integer with leading zeros to an Integer field in request payload in Spring boot?

Ask Question

However, expMonth is not binding properly and throws an exception. Please suggest the way forward.

Exception Stack:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid numeric value: Leading zeroes not allowed; nested exception is com.fasterxml.jackson.core.JsonParseException: Invalid numeric value: Leading zeroes not allowed at [Source: (PushbackInputStream); line: 4, column: 16]

Could you include the stack trace and your implementation? It's hard to tell what's going wrong without looking at the trace. user2004685 Jun 25, 2019 at 18:08 org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid numeric value: Leading zeroes not allowed; nested exception is com.fasterxml.jackson.core.JsonParseException: Invalid numeric value: Leading zeroes not allowed at [Source: (PushbackInputStream); line: 4, column: 16] Sumanth Varada Jun 25, 2019 at 18:12

As per the exception stack, you have leading zeros and hence Jackson is thrwing the exception. You can either try something like,

"name": "XXXX", "expMonth": 7, <--- Removing the leading zero(s). "expYear": 21

Another way is to change it to String instead,

"name": "XXXX", "expMonth": "07", "expYear": 21

Code:

public Class Card {
     private String name;
     private String expMonth;
     private Integer expYear;

You can then do something like,

Integer.parseInt(expMonth);
                I know that String is an easy option. However, this payload has to send to someother api. I just want to avoid the conversion. I'm using ObjectMapper here.
– Sumanth Varada
                Jun 25, 2019 at 18:24
                @SumanthVarada A leading 0 indicates an octal number. Hence, leading zeros are not allowed for numeric values in JSON. See this
– user2004685
                Jun 25, 2019 at 18:29
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.