0

How to take Date time as a input in request body of a post mapping controller.

This is my controller which is taking request body of a object DTO.

@PostMapping(value = "/filterJobs")
    @ResponseStatus(HttpStatus.OK)
    public ApiResponse getFilteredJobs(@RequestParam(defaultValue = "0") Integer pageNo,
                                       @RequestParam(defaultValue = "20") Integer pageSize,
                                       @RequestBody FilteredJobsDto filteredJobsDto) {

        Code here but its coming even to the first line of code
    }

I am taking FilteredJobsDto in request body which has following parameters

public class FilteredJobsDto implements Serializable {

    private String jobName;

    private Long createdBy;

    private String status = "submitted";

    private String tenantId;
   
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    @JsonDeserialize(using = DateTimeDeserializer.class)
    private DateTime startDate;
    
    @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private DateTime endDate;
}

My json body looks like this as of now:

{
    "jobName": "email notification",
    "startDate": "2023-03-02T08:20:11.000Z",
    "status": "submitted"
}

The error i am facing here is a bad request in postman and on the console side :

logException : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 3 column 19 path $.startDate; nested exception is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 3 column 19 path $.startDate]

When i am not taking startDate in json body, the api is working fine, but as soon as i put "startDate": "2023-03-02T08:20:11.000Z" this in my json body, it throws an error.

Can someone please help me resolve this!

3 Answers3

0

Can you please try this? I have the same request body and below works fine for me

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]XXX")
private DateTime startDate;

Edit

  import java.time.ZonedDateTime;
  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]XXX")
  private ZonedDateTime startOn;
0

It looks like your are using some outdated classes. In the current Java package java.time you will find everything you need:

import java.time.LocalDateTime;

private LocalDateTime startDate;
Mar-Z
  • 2,660
  • 2
  • 4
  • 16
  • Hey @Mar-z, I tried this - ( @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") private LocalDateTime startDate;) But this also giving the same error, i also tried without "@DateTimeFormat", but still the same – Sahil Ranjan Apr 25 '23 at 05:32
  • Before answering I have tested it with the newest version of Spring Boot which uses Jackson framework 2.14.2. It works just fine even without DateTimeFormat annotation. Because your data match default ISO format. Otherwise the format pattern would be necessary. – Mar-Z Apr 25 '23 at 08:14
-1

Spring Boot provides good date conversion by default. You can achieve this using LocalDateTime without any additional dependencies.

public class Sample {
    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    LocalDateTime startDate;
}

By default, Jackson cannot use Joda-Time. You need to register the JodaModule in order to use Joda-Time with Jackson. You can add this configuration to let Spring Boot register it at startup.

@Configuration
public class JacksonConfiguration {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.modules(new JodaModule());
    }
}

After registering the Joda module in the ObjectMapper, you can use it to convert the request to the module.

public class Sample {
    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    DateTime startDate;
}
Ziv
  • 119
  • 7
  • 1
    Joda is not needed anymore. LocalDateTime is now part of standard Java in the java.time package. – Mar-Z Apr 24 '23 at 15:18
  • Yes, Thank you for @Mar-Z supplement. I added a relatively good solution. – Ziv Apr 24 '23 at 15:28
  • Hey @Ziv tried the first one, but still the error is coming, regarding the second option, after adding the following in config file, do i also need to do something at the controller level or just adding the config file will work! – Sahil Ranjan Apr 25 '23 at 07:03