0

Here is a code snippet:

@GetMapping("/account")
@SuppressWarnings("unchecked")
public UserDTO getAccount(Principal principal) {
    ...
    janitorService.cleanUp((AbstractAuthenticationToken) principal);
    ...
}

@Component
public class JanitorService {
  ...
  @Async
  public boolean cleanUp(AbstractAuthenticationToken authToken){
    ...
    return true;
  }

}

and there is an async configuration class.

org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public boolean com.mycompany.myteam.JanitorService.cleanUp(org.springframework.security.authentication.AbstractAuthenticationToken) 

The exception won't be thrown after @Async is removed. The reason I use @Async is to kick off a thread. Why does it cause the exception?

vic
  • 2,548
  • 9
  • 44
  • 74
  • Does this answer your question? [Will an aspect be executed asynchronously if I put @Async method on it?](https://stackoverflow.com/questions/58275121/will-an-aspect-be-executed-asynchronously-if-i-put-async-method-on-it) – kriegaex Sep 25 '21 at 08:34
  • Not on the same topic, but it is nice to learn some related knowledge. – vic Sep 25 '21 at 17:56
  • It is the same topic, I am explaining exactly why your exception occurs. – kriegaex Sep 25 '21 at 21:07
  • I have another look and see your point. Thanks. – vic Sep 26 '21 at 00:11

1 Answers1

0

For @Async methods with return, the return should be a Future<T> not a T. Try returning Future<Boolean> Source: https://www.baeldung.com/spring-async

leoconco
  • 253
  • 3
  • 15