title: How to make a async REST with Spring?
body: <p>I'm trying to make a small REST using Spring Boot.
  I've never used Spring and used Java a long time ago (Java 7)!</p>
  
  <p>In the last 2 years I have used only Python and C# (but like I said, I already used Java).</p>
  
  <p>So, now, I'm trying to make a REST using async methods, and checked several examples, but still, I don't understand very well the "correct way" to do this.</p>
  
  <p>Looking at the following documentation: <a href="http://carlmartensen.com/completablefuture-deferredresult-async" rel="noreferrer">http://carlmartensen.com/completablefuture-deferredresult-async</a>, Java 8 has <code>CompletableFuture</code> that I can use with Spring, so, I made the following code:</p>
  
  <p><strong>Service</strong>:</p>
  
  <pre><code>@Service
  public class UserService {
    private UserRepository userRepository;
  
    // dependency injection
    // don't need Autowire here
    // https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-spring-beans-and-dependency-injection.html
    public UserService(UserRepository userRepository) {
      this.userRepository = userRepository;
    }
  
    @Async
    public CompletableFuture&lt;User&gt; findByEmail(String email) throws InterrupedException {
      User user = userRepository.findByEmail(email);
      return CompletableFuture.completedFuture(user);
    }
  }
  </code></pre>
  
  <p><strong>Repository</strong>:</p>
  
  <pre><code>public interface UserRepository extends MongoRepository&lt;User, String&gt; {
    @Async
    findByEmail(String email);
  }
  </code></pre>
  
  <p><strong>RestController</strong>:</p>
  
  <pre><code>@RestController
  public class TestController {
  
    private UserService userService;
  
    public TestController(UserService userService) {
      this.userService = userService;
    }
  
    @RequestMapping(value = "test")
    public @ResponseBody CompletableFuture&lt;User&gt; test(@RequestParam(value = "email", required = true) String email) throws InterruptedException {
      return userService.findByEmail(email).thenApplyAsync(user -&gt; {
        return user;
      })
    }  
  }
  </code></pre>
  
  <p>This code give me the expected output.
  Then, looking at another documentation (sorry, I lost the link), I see that Spring accept the following code (which give me the expected output too):</p>
  
  <pre><code>  @RequestMapping(value = "test")
    public @ResponseBody CompletableFuture&lt;User&gt; test(@RequestParam(value = "email", required = true) String email) throws InterruptedException {
      return userService.findByEmail(email);
    }  
  }
  </code></pre>
  
  <p>Is there a difference between the two methods?</p>
  
  <p>Then, looking at the following guide: <a href="https://spring.io/guides/gs/async-method/" rel="noreferrer">https://spring.io/guides/gs/async-method/</a>, there's a <code>@EnableAsync</code> annotation in <code>SpringBootApplication</code> class.
  If I include the <code>@EnableAsync</code> annotation and create a <code>asyncExecutor</code> Bean like the code from last link, my application don't return nothing on <code>/test</code> endpoint (only a 200 OK response, but with blank body).</p>
  
  <p>So, my rest is async without the <code>@EnableAsync</code> annotation?
  And why when I use <code>@EnableAsync</code>, the response body is blank?</p>