No converter found capable of converting from type [java.lang.String] to type [java.util.LinkedHashMap<java.lang.String, java.lang.String>]

Viewed 12988

spring boot 2.1.1 can not read yml config to LinkedHashMap

this is my class

I've provided get and set functions. But it doesn't work.

@Getter
@Setter
@Configuration
@ConfigurationProperties("shiro")
public class ShiroConfiguration {
    private LinkedHashMap<String, String> filterChainDefinitions;
}

this is my config

shiro:
  filterChainDefinitions:
    /**: origin

this is error message

Description:

Failed to bind properties under 'shiro.filter-chain-definitions' to java.util.LinkedHashMap<java.lang.String, java.lang.String>:

    Reason: No converter found capable of converting from type [java.lang.String] to type [java.util.LinkedHashMap<java.lang.String, java.lang.String>]

Action:

Update your application's configuration
5 Answers

@ConfigurationProperties can't map LinkedHashMap from you properties. You have to use SpEL which is not supported as mentioned here

To solve your issue could update the configuration by adding @Value annotation eg.:

@Getter
@Setter
@Component
public class ShiroConfiguration {

  @Value("#{${shiro.filter-chain-definitions}}")
  private LinkedHashMap<String, String> filterChainDefinitions;

}

The special characters(/, *) in the key are throwing the binding off. Map keys with special characters need to be surrounded with square brackets. The below should work:

shiro:
  filterChainDefinitions:
    '[/**]': origin

You can use configuration properties. Ex

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "shiro")
class YourComponent {

    private HashMap<String, Integer> filterChainDefinitions;

    public HashMap<String, Integer> getFilterChainDefinitionsta() {
        return filterChainDefinitions;
    }

    public void setFilterChainDefinitions(HashMap<String, Integer> filterChainDefinitions) {
        this.filterChainDefinitions= filterChainDefinitions;
    }

}

This might be an issue with the way you are calling the configuration property.

Have you tried renaming your property path:

filterChainDefinitions to filter

and

private LinkedHashMap<String, String> filterChainDefinitions;
to
private LinkedHashMap<String, String> filter;?

Such errors could also occur sometimes due to a wrong encoding or escaping but this doesn't seem to be the case here.

Try using block mappings like this:

shiro:
  filterChainDefinitions:
    {}

When the value of shiro.filterChainDefinitions is empty it will be interpreted as an empty string. There's no way for YAML to know it's supposed to be a LinkedHashMap. Use an empty block mapping to represent the empty Hashmap.

From the docs: A Block mapping is a series of entries, each presenting a key: value pair.

Related