springspring-aopspring-asyncspring-aspects

Why @Async Annotation Clauses AopInvocationException?


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?


Solution

  • 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