0

My problem is that I need to deserialize my json response from the server into a java object, but the problem is that after deserialization, there are only null in the set fields. The answer from the server checked the filled fields come. What am I doing wrong and how to fix it? Thanks for the help.

This my method service

 public ReportDataFromReporter getReports(int weekNumber, int year) throws JsonProcessingException {
    LocalDate mondayOfWeek = LocalDate.of(year, Month.JUNE, 1)
            .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
            .with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber);
    LocalDate sundayOfWeek = mondayOfWeek.plusDays(6);
    String url = "example.com";
    ObjectMapper objectMapper = new ObjectMapper()
            .registerModule(new JavaTimeModule());
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    objectMapper.setDateFormat(dateFormat);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ObjectForRequestToReporter objectForRequestToReporter = new ObjectForRequestToReporter();
    objectForRequestToReporter.setToken(userService.getUser().getReporterToken());
    objectForRequestToReporter.setEmployee(userService.getUser().getLogin());
    objectForRequestToReporter.setFrom(mondayOfWeek);
    objectForRequestToReporter.setTo(sundayOfWeek);
    String jsonObject = objectMapper.writeValueAsString(objectForRequestToReporter);
    System.out.println(jsonObject);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>(jsonObject, httpHeaders);
    String reporterResponse = restTemplate.postForObject(url, entity, String.class);
    ReportDataFromReporter reportDataFromReporter = objectMapper
            .readValue(reporterResponse, ReportDataFromReporter.class);
    System.out.println(reportDataFromReporter);
    return reportDataFromReporter;

this is the Java object I need to deserialize into

 @Data
 public class ReportDataFromReporter {
private Long id;
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate day;
private Integer hours;
private String description;
private Long project;
private Long taskType;
private Long work_after;
 }

This response from external api in json

 {
   "reports": [
     {
      "id": somenumber,
  "date": "2023-03-15T00:00:00+03:00",
  "hour": "sometext",
  "text": "sometext",
  "pro": somenumber,
  "task": somenumber,
},
{
  "id": somenumber,
  "date": "2023-03-15T00:00:00+03:00",
  "hour": "sometext",
  "text": "sometext",
  "pro": somenumber,
  "task": somenumber,
},
{
   "id": somenumber,
  "date": "somedate",
  "hour": "sometext",
  "text": "sometext",
  "pro": somenumber,
  "task": somenumber,
},
{
"id": somenumber,
  "date": "2023-03-15T00:00:00+03:00",
  "hour": "sometext",
  "text": "sometext",
  "pro": somenumber,
  "task": somenumber,
     }
   ]
 }
Khaski
  • 29
  • 5

1 Answers1

1

None of your properties of your class match the name of your files in Json. Also the date field in Json should be mapped to OffsetDateTime type or ZonedDateTime. To properly map your date field see the answer to this question for more details: Spring Data JPA - ZonedDateTime format for json serialization. As for the unmatching properties to field names you should use @JsonProperty annotation. For instance instead of

private Long project;

use

@JsonProperty("pro")
private Long project;

This will tell ObjectMapper to map field "pro" to property "project". See this article: Jackson Annotation Examples

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • i have a problem not only with date, after desirialize every fields is null – Khaski May 22 '23 at 12:31
  • I updated my answer to address your problem – Michael Gantman May 22 '23 at 13:04
  • I added annotation @JsonProperty for every field in my java object, but there still is null – Khaski May 23 '23 at 13:36
  • 1
    Your Json string is a list and not a single value. So, try to change the line: ReportDataFromReporter reportDataFromReporter = objectMapper .readValue(reporterResponse, ReportDataFromReporter.class); to List = objectMapper .readValue(reporterResponse, List.class); But I suspect that you will get List and not List. So in this case you will need to go over every map in the list and convert it to ReportDataFromReporter – Michael Gantman May 23 '23 at 14:04
  • can u help one more time how i can convert it to ReportDataFromReporter? – Khaski May 24 '23 at 14:38
  • If you get a list of maps, the simple way would be to add a constructor in your class ReportDataFromReporter that takes a map as a parameter and sets all its properties from values it reads from the map. – Michael Gantman May 24 '23 at 15:05