skip to Main Content

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

Answers


  1. 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;
    
    Login or Signup to reply.
  2. 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;
    }
    
    Login or Signup to reply.
  3. 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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search