使用DeferredResult时,ResponseEntity获取了null body

时间:2017-03-15 02:48:42

标签: java rest spring-boot

我正在使用SpringBoot 1.4.4创建一个异步RESTful api。一切正常,除非成功处理请求,DeferredResult< ResponseEntity< String>>即使我明确地将字符串值设置为结果对象,也始终具有null body和200 OK状态代码。代码如下所示:

RestController impl:

 @Async
  public DeferredResult<ResponseEntity<String>> parseWithLongDelay(    
     @RequestBody MyRequestObject req){
     DeferredResult<ResponseEntity<String>> result = new DeferredResult<>();
     :
     :
     result.setResult(new ResponseEntity<String>("That big zip file successfully parsed.", HttpStatus.OK));
     return result;
  }

客户方:

 @Test
 public void test(){
    ListenableFuture<ResponseEntity<String>> futureEntity = new AsyncRestTemplate().postForEntity(url, requestEntity,
    String.class);
    futureEntity.addCallback(new MyFutureCallback());
    :
  }
  class MyFutureCallback implements ListenableFutureCallback<ResponseEntity<String>> {
  @Override
  public void onSuccess(ResponseEntity<String> entity) {
   String body = entity.getBody();
   log.info("response content: " + body);
   //always get null body, there is no server side exception or error
   //when callback happens
 }
}  

我期待字符串“那个大的zip文件被成功解析了。”身体得到返回而不是null?任何线索?请帮忙!

1 个答案:

答案 0 :(得分:0)

我发现在我的情况下ShallowEtagHeaderFilter引起了故障。这是我在我的rest方法中为避免空体所做的事情:

ShallowEtagHeaderFilter.disableContentCaching(request);