postmanobjectmapperjackson-databindlocaldate

How to solve error Code 45 : serialize/deserialize LocalDate springboot using Json ObjectMapper


Below is the error message I get :

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)): Expected space separating root-level values
 at [Source: (String)"2018-10-10"; line: 1, column: 6]

Below is my UPDATED code to read date from post man Json: { "modifiedDate":"2018-09-10"}

String modifiedDateJson = data.get("modifiedDate").asText();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(modifiedDateJson);
        String output = dateFormat.format(date);


        Date modifiedDateDf = mapper.readValue(modifiedDateJson,Date.class);
        LocalDate modifiedDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

Solution

  • Okay i got the answerto my problem of error code 45; answer is as simple as ABC;

    ( thanks for your time those of you who tried your levels best ... )

    And please excuse my answer/code for not being indented; am using a phone to post this answer...and I hope it helps someone.😊

    First thing .

    1. Receive date:

      String modifiedDate = data.get ("modifiedDate").asText ();

    Second thing...

    1. Parse local date:

      LocalDate dateToSave = LocalDate.parse(modifiedDate);

      That's it ... 😆😊

    See also for more insights: https://www.mkyong.com/java8/java-8-how-to-convert-string-to-localdate/