Spring Boot 2.x list binding issues


Spring Boot 2.x list binding issues



I'm having issues with binding some Lists after upgrading to Spring Boot 2x. The code worked in Spring 1.x and now it throws a binding error on startup. Here's my application.yml...


aws:
geo-mappings:
- name: USA
regions:
- us-west-2
- us-west-1
- us-east-1
- us-east-2
- name: California
regions:
- us-west-2



Here's my component class...


package com.example.demo.config.aws;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
* Created by goer on 4/18/17.
*/
@Component
@Scope("singleton")
@ConfigurationProperties(prefix="aws")
public class AWSConfigProvider {

private List<GeoMappingEntry> geoMappings = new ArrayList<>();

public List<GeoMappingEntry> getGeoMappings() {
return this.geoMappings;
}


}



Here's the nested object...


package com.example.demo.config.aws;

import java.util.ArrayList;
import java.util.List;


public class GeoMappingEntry {
private String name;
private List<String> regions = new ArrayList<>();

public GeoMappingEntry(String name, List<String> regions) {
this.name = name;
this.regions = regions;
}
}



When I try to run I get...



APPLICATION FAILED TO START



Description:



Failed to bind properties under 'aws.geo-mappings' to java.util.List:


Reason: Failed to bind properties under 'aws.geo-mappings' to java.util.List<com.example.demo.config.aws.GeoMappingEntry>



Action:



Update your application's configuration



Has anybody else run in to the same problem? Solutions? Suggestions?





I want to say that your name field needs a setter. Initialized collections don't need setters, but the uninitialized field may need one.
– Compass
Jun 8 at 19:03


name





might be a bug fixed in 2.0.1
– mavriksc
Jun 8 at 21:44





I wish. I've created a simplified project using 2.0.2 which is the latest version and it's not working. I suspect that the new binding implementation has tighter restrictions around something and has created the issue.
– Aaron Cooley
Jun 8 at 23:50




1 Answer
1



Just incase anybody else is looking at a similar issue. It turned out that some environment variables that didn't work in previous versions of Spring are now able to actually create bindings and that's were the errors were coming from.



In this case relaxed binding from an environment var to a list such as AWS_GEOMAPPINGS_0_REGIONS_0 = us-west-2 wasn't working before but now it does. Before Spring Boot 2.0 the only way to set this from an environment var was to pass JSON via SPRING_APPLICATION_JSON, which works, but gets complicated if you are trying to deploy using some other JSON that encapsulates it (like Terraform).


AWS_GEOMAPPINGS_0_REGIONS_0 = us-west-2


SPRING_APPLICATION_JSON





Can you provide more details? It would be useful for future readers to know, for instance, which environment variables are the one you're talking about... just saying.
– lealceldeiro
Jul 2 at 18:41






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

JMeter fails on beanshell imports

Why in node-red my HTTP POST no receive payload from inject?

PHP contact form sending but not receiving emails