Spring @RequestParam DateTime format as ISO 8601 Date Optional time

I use the Spring Framework for my services API and org.joda.time.DateTimefor datetime parsing. In particular, I use ISOFormatter.dateOptionalTimeParser(), which allows users to flexibly use only the date or date and time, which is a requirement.

Believe me, I saw all these related questions that I can already say, people are going to point me, for example. and so on

I used to take the date as String, and then process it using the joda formatting mentioned above at the service level, but now I want to add the request check to the controller, which means that if the request is syntactically incorrect, the request should not even go to the service level.

I tried using several options @DateTimeFormat(iso = ISO.DATE_TIME)as well as specifying patternString in a no-luck format.

@RequestMapping(value = URIConstants.TEST_URL, method = RequestMethod.GET)
public @ResponseBody String getData(@RequestParam(required = false) DateTime from,
                                    @RequestParam(required = false)  DateTime to)  {
    return dataService.fetchDataFromDB(from, to);
}

What should I do to ensure that the date I receive from the user matches the format ISO 8601 dateOptionalTime? Can I apply multiple patterns to implement this?

+4
source share
2 answers

You can also create a converter and it will take care of it. I used OffsetDateTime in the example below, but it can easily be replaced with LocalDateTime. For a detailed article, refer to this URL - http://www.baeldung.com/spring-mvc-custom-data-binder

- , . @Component .

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class OffsetDateTimeConverter implements Converter<String, OffsetDateTime> {

    @Override
    public OffsetDateTime convert(final String source) {

        if (source == null || source.isEmpty()) {
            return null;
        }

        return OffsetDateTime.parse(source, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    }
}
+2

POJO , String ISODateTimeFormat.dateOptionalTimeParser():

public class DateOptionalTime {

    private DateTime date;

    private DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser();

    public DateOptionalTime() {
    }

    public DateOptionalTime(String date) {
        if (date != null) {
            this.setDate(formatter.parseDateTime(date));
        }
    }

    public DateTime getDate() {
        return date;
    }

    public void setDate(DateTime date) {
        this.date = date;
    }

    public LocalDateTime getLocalDateTime() {
        return date.toLocalDateTime();
    }


}

:

@RequestMapping(value = URIConstants.TEST_URL, method = RequestMethod.GET)
public @ResponseBody String getData(@RequestParam(required = false) DateOptionalTime from,
                                    @RequestParam(required = false)  DateOptionalTime to)  {
    return dataService.fetchDataFromDB(from, to);
}

ISO BAD_REQUEST, .

+2

All Articles