How to enable 'ALLOW_NUMERIC_LEADING_ZEROS' feature to allow leading zeroes in JSON Request Body?

前端 未结 4 1110

As per JSON specification, I am aware that leading zeroes are not allowed in integers in JSON. But as per Jackson documentation, there is a property in Jackson library i.e.

4条回答
  •  遥遥无期
    2020-12-21 19:55

    Just a note for people that are having that issue and are looking for a newer working solution:

    1. Import latest version of fasterxml jackson in maven (2.11.0 as of today):

      com.fasterxml.jackson.dataformat jackson-dataformat-xml 2.11.0

    2. Create the mapper object:

      ObjectMapper objectMapper = new ObjectMapper();

    3. Allow the leading zeros for numbers (the not deprecated version):

      objectMapper.enable(JsonReadFeature.ALLOW_LEADING_ZEROS_FOR_NUMBERS.mappedFeature());

    used imports:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.core.json.JsonReadFeature;
    

    Keep in mind this will trim the leading 0s. If you want to keep them then your json value shouldn't be a numeric.

提交回复
热议问题