'How to call a method on a dependency with SpEL
I want to call a method on a dependency in my controller in @PreAuthorize annotation using SpEL, but I cannot figure it out, I tried a bunch of things but cannot get it to work.
The method looks something like this (SpEL is wrong here):
@Override
@GetMapping("/count")
@PreAuthorize("@puppetSecurity.isPuppetCountAllowed(puppetMapper.toEntity(#puppetType))")
public ResponseEntity<PuppetCountDto> getPupetCount(PuppetType puppetType) {
// CODE
}
I want to call the puppetMapper dependency's method toEntity and convert the parameter of the endpoint method. How would I do that?
Solution 1:[1]
Problem was I wanted to reference a MapStruct mapper, which was registered as a bean, with the name of the interface instead of the actual bean name which is the implementation.
I had a mapper interface called PuppetMapper, registered as a Spring bean, but looking at the actual implementation, the name I had to reference was PuppetMapperImpl.
So the PreAuthorize SpEL would look like this:
@PreAuthorize("@puppetSecurity.isPuppetCountAllowed(@puppetMapperImpl.toEntity(#puppetType))")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | D.Tomov |
