1

I am trying to load custom properties from a YAML properties file upon initialization using Spring Boot. I have found countless tutorials on how to do that, and they work. The problem is that I can't seem to find a way how to instantiate POJOS, such as for example LocalDateTime. My code is as shown below.

@Configuration
@ConfigurationProperties(prefix="default-film-showings")
public class FilmShowings {
    private List<FilmShowing> filmShowings;

    //Constructors, Getters, setters etc.

    public static class FilmShowing {
        private Integer id;
        private Film film;
        private Theatre theatre;
        private LocalDateTime dateTime;

        //Constructors, Getters, setters etc.
    }
}

My YAML file is currently as follows

default-film-showings:
  filmShowings:
    - id: 1
      dateTime: 2018-07-13 21:00:00

My problem is that I get the following error upon initialisation

Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'filmShowings[0].dateTime';

I also tried this variant

default-film-showings:
  filmShowings:
    - id: 1
      dateTime:
        date: 
          year: 2018
          month: 7
          day: 13
        time:
          hour: 21
          minute: 0
          second: 0
          nano: 0

but I got the following error

Error creating bean with name 'filmShowings': Could not bind properties to FilmShowings

Any help? I looked at the following thread JSON Java 8 LocalDateTime format in Spring Boot but it didn't solve my problem.

On a similar note, is there any way to link the Film POJO attribute to another default property?

Say I have the following in my properties file

default-films:
  films:
    - id: 1
      filmName: Spider-Man

can I add something like this too?

default-film-showings:
  filmShowings:
    - id: 1
      film: default-films.films[0]
      dateTime: whatever I need to do here to make it work

It is reading default-films.films[0] as string and thus not matching 'the YAML' object.

Any help?

Nik
  • 355
  • 1
  • 7
  • 18
  • What happens if you try with the string in ISO format. Example `2007-03-01T13:00:00` ? – Rafa Jul 08 '17 at 17:41
  • Hi. Thank you for your answer. I currently have someting like this `@DateTimeFormat(pattern = "yyyy-MM-ss HH:mm:dd", iso= DateTimeFormat.ISO.DATE_TIME) private LocalDateTime dateTime;` and in the YAML i put `dateTime: "2007-03-01T13:00:00"`. Got the following error `Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime'` – Nik Jul 08 '17 at 17:49

2 Answers2

4
  1. Assuming Spring honors the @DateTimeFormat annotation in a @ConfigurationProperties class (I haven't verified that), you've declared the format to be DateTimeFormat.ISO.DATE_TIME which is 2011-12-03T10:15:30+01:00[Europe/Paris]. Your property file has 2018-07-13 21:00:00 which isn't any of the standard formats. How do you expect it to work?

  2. Your datatype is LocalDateTime which doesn't have a concept of time zone, thus contradicting the ISO_DATE_TIME format. However, the time zone is optional is for ISO_DATE_TIME, so this may not be an issue.

Clearly, you're throwing spaghetti at the wall and hoping something would stick. Write a unit test and try to convert the string to whatever datetime object you want. Once you've done that, come back if you've a problem.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
-1

You should just adapt the setter in the configuration class.

if your yaml is:

default-film-showings:
  filmShowings:
    - id: 1
      dateTime: 2018-07-13 21:00:00

then do this in your configuration class:

@Configuration
@ConfigurationProperties(prefix="default-film-showings")
public class FilmShowings {
    private List<FilmShowing> filmShowings;

    //Constructors, Getters, setters etc.

    public static class FilmShowing {
        private Integer id;
        private Film film;
        private Theatre theatre;
        private LocalDateTime dateTime;

        //Constructors, Getters, setters etc.
        public void setDateTime(String dateTime) {
          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 
          HH:mm:ss");
          LocalDateTime formatDateTime = LocalDateTime.parse(dateTime, formatter);
          this.dateTime= formatDateTime;
        }
    }
}