Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this stream & lambda expression work with SpEL declaration?

I'm trying to use a Java 8 stream and lambda expression in a Spring @Cache annotation.

I'm trying to use the following:

@CacheEvict(value = "tags", allEntries = true, 
condition = "#entity.getTags().stream().anyMatch(tag -> tag.getId() == null)")

It is failing with:

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
org.springframework.expression.spel.SpelParseException: 
EL1042E:(pos 40): Problem parsing right operand

However, I am able to get it to work if I move the stream into a method on the entity. The annotation then works as follows without error:

@CacheEvict(value = "tags", beforeInvocation=true, allEntries = true, 
condition = "#entity.containsNewTag()")

I would prefer not to need the 'containtsNewTag()' method and use the stream directly in the SpEL expression if possible. Can this be done?

like image 256
Justin Avatar asked Aug 21 '15 04:08

Justin


People also ask

Why are streams not working?

Internet buffering problems are usually caused by one of three issues. Your internet connection is too slow to keep up with the incoming data. The streaming provider can't send your device the data it needs fast enough. Your home Wi-Fi network is slowing things down.

Why can I not live stream?

Your channel's live streaming ability will be automatically turned off or suspended for any of the following reasons: Your channel got a Community Guidelines strike. Your live stream or archived live stream gets a copyright takedown. Your live stream matches another copyrighted live broadcast.

Why do some Twitch streams not load?

Why Are Twitch Streams Not Loading? Assuming that Twitch (a subsidiary of Amazon) does not have any server issues, the reason why the streams you're trying to watch are not loading is that you have a slow Internet connection on your side.


2 Answers

Spring Expression Language is defined in the developer guide. What you're trying to do isn't supported by the language at the moment. I'd also argue that this is a very weird place to put such code: an isolated method that you can unit test is much better indeed.

like image 88
Stephane Nicoll Avatar answered Oct 20 '22 05:10

Stephane Nicoll


Don't let naysayers get in your way, even if they author accepted answers! :) You can accomplish your intention with a little creativity! The following syntax (using Collection Selection and 'this')

here #root is your entity, and inside the selection, #this refers to a tag.

Example anyMatch:

"#root.getTags().?[#this.getId() == null].size() > 0"

Example allMatch:

"#root.getTags().?[#this.getId() == null].size() eq #root.getTags().size()"
like image 28
mancini0 Avatar answered Oct 20 '22 05:10

mancini0