Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to convert String to Date by requestBody in spring

I have the below Code :

DTO :

 Class MyDTO {
        import java.util.Date;
        private Date dateOfBirth;

        public Date getDateOfBirth() {
                return dateOfBirth;
            }
        public void setDateOfBirth(Date dateOfBirth) {
                this.dateOfBirth = dateOfBirth;
            }

    }

Controller

public void saveDOB(@RequestBody MyDTO myDTO, HttpServletRequest httprequest, HttpServletResponse httpResponse) {
       System.out.println("Inside Controller");
       System.out.println(myDTO.getDateOfBirth()); 
}

JSON Request :

{
"dateOfBirth":"2014-09-04",

}

If I send the request as yyyy-mm-dd automatic conversion to date object happens. output in controller:- dateOfBirth= Thu Sep 04 05:30:00 IST 2014

But when I send DateofBirth in dd-mm-yyyy format It does not convert String to Date automatically.So how i can i handle this case.

JSON Request :

{
"dateOfBirth":"04-09-2014",

}

Output: No Output in console does not even reaches controller.

I have tried with @DateTimeFormat but its not working.

I am using Spring 4.02 Please suggest is there any annotation we can use.

like image 975
user3745727 Avatar asked Sep 03 '14 14:09

user3745727


People also ask

How to convert string parameters to date in Spring Boot?

This is because Spring by default cannot convert String parameters to any date or time object. 3. Convert Date Parameters on Request Level One of the ways to handle this problem is to annotate the parameters with the @DateTimeFormat annotation and provide a formatting pattern parameter:

How to get the date from a string in spring?

TL;DR - you can capture it as a string with just @RequestParam, or you can have Spring additionally parse the string into a java date / time class via @DateTimeFormat on the parameter as well. the @RequestParam is enough to grab the date you supply after the = sign, however, it comes into the method as a String.

Is it possible to pass date/date-time in empty values in Spring Boot?

The example that they provide should have X instead of Z for its pattern as they included -05:00 as opposed to -0500. I tried this solution and it works if you pass date or DateTime, but when the values are EMPTY, this is failing. I found workaround here. Spring/Spring Boot only supports the date/date-time format in BODY parameters.

How to handle date and time conversion in Spring MVC?

Another way to handle date and time object conversion in Spring is to provide a global configuration. By following the official documentation, we should extend the WebMvcConfigurationSupport configuration and its mvcConversionService method:


1 Answers

List itemCreate a class to extend JsonDeserializer

public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String date = jsonParser.getText();
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}

Use @JsonDeserialize(using = CustomJsonDateDeserializer.class) annotation on setter methods.

Thanks @Varun Achar answer, url

like image 81
Jaille Chen Avatar answered Oct 15 '22 16:10

Jaille Chen