spring无阻碍rest“发送并忘记”

我正在编写一个无阻塞的Spring Rest控制器。 我的客户应发送请求,不关心响应,也不需要等待。

这是我的服务器代码:

@RestController @EnableAsync public class testController { @RequestMapping(value = "test", method = RequestMethod.GET) public ResponseEntity test() throws InterruptedException { timeConsumingMethod(); System.out.println("I'm should be first"); return new ResponseEntity("the server is processing your request", HttpStatus.OK); } @Async private void timeConsumingMethod() throws InterruptedException { Thread.sleep(1000*5); System.out.println("I'm should be second!"); } 

但是,当我使用(POSTMAN,Chrome等)调用http:// localhost:8181 / test时 ,我在服务器日志中得到以下信息:

我应该是第二名!

我应该是第一个

并且等待5秒后我的浏览器显示:

服务器正在处理您的请求

这是“发送和忘记”行为的正确方法吗?

根据doc页面,应该在配置类上添加@EnableAsync

启用S​​pring的异步方法执行function,类似于Spring的XML命名空间中的function。

要在@Configuration类上使用,如下所示,其中MyAsyncBean是一个用户定义的类型,其中一个或多个方法使用Spring的@Async批注,EJB 3.1 @ javax.ejb.Asynchronous批注或通过批注指定的任何自定义批注进行批注()属性。