yathirigan
yathirigan

Reputation: 6069

Find values in a Map using SpEL

We have a Map containing configurations as follows.

Class Config {
     Map<String, Boolean> enabled = new LinkedHashMap<String, Boolean> ();

The config POJO object is populated from the Spring Cloud Config Server.

The Key value is arrived based on what functionality the user is accessing now.It will have key-values like {'default': true,'login':false,'dashboard':true} }

In my Servlet Filter, I want to take the value for a given key (based on user accessing functionality), if key not present, I want to take the 'default' value.

I can write a ternary operator by using map.get to achieve this but, can i do the same using SpEL (Spring Expression Language) ?

boolean isEnabled = configuration.getEnabled().get(api) == null ? configuration.getEnabled().get("default") : configuration.getEnabled().get(api);

Upvotes: 0

Views: 3815

Answers (1)

Gary Russell
Gary Russell

Reputation: 174664

It's not entirely clear what you are asking but...

"['foo']?:['default']"

...if the map contains key foo, its value is returned, otherwise the value for default.

(This assumes the map is the #root object for the evaluation).

See the Elvis Operator.

Upvotes: 1

Related Questions