Home > OS >  How to call a method on a dependency with SpEL
How to call a method on a dependency with SpEL

Time:06-08

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?

CodePudding user response:

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))")
  • Related