浏览器/邮递员不等待@Async Spring MVC控制器响应?

时间:2016-04-11 14:45:55

标签: angularjs spring spring-mvc

我正在使用SpringMVC和AngularJS。我做了一个我想要异步调用的控制器但是当我从Postman发送POST请求进行测试时,它会立即返回而不等待响应。我也试过Callable,Executor,但没有任何作用。当我删除@Async时它工作正常。任何人都可以帮忙吗?

@RestController
public class GenServiceController {

@Autowired
private GenService service;

@RequestMapping(value = "/log", method = RequestMethod.POST)
@ExceptionHandler(ForbiddenErrorCodeException.class)
@ResponseBody
@Async
public DeferredResult<CustomResponse> log(@RequestBody TestInfo testInfo, 
                            @RequestHeader(value = "Authorization") String accessToken) throws ForbiddenErrorCodeException {

    String decodedAccessToken = null;
    CustomResponse customResponse = null;
    final DeferredResult<LogResponse> deferredResult = new DeferredResult<>();
    if (!Utils.isNullOrTrimEmpty(accessToken)) {
        accessToken = accessToken.replaceFirst("Basic" + " ", "");
        decodedAccessToken = Utils.decodeBase64(accessToken);
        final StringTokenizer tokenizer = new StringTokenizer(decodedAccessToken, ":");
        final String token = tokenizer.nextToken();

        if (!service.isTokenExists(token)) {
            System.out.println("Invalid Token");
        } else {
            response = service.sendMessages(testInfo);
            if (response != null) {
                deferredResult.setResult(response);
            }
        }
    }
    return deferredResult;
}

}

1 个答案:

答案 0 :(得分:0)

您无需使用Async注释您的方法 - 它在您的方案中没有意义。只需返回DeferredResult<CustomResponse>Callable<CustomResponse>