2015-10-06 63 views
2

我試圖將HATEOAS應用於我的彈簧引導應用程序,使用spring-hateoas。這工作得很好,直到我包裹着我的REST調用內HystrixCommand:如果我評論了HystrixCommand標註出Could not find current request via RequestContextHolderHystrixCommand註釋導致ControllerLinkBuilder中斷

@HystrixCommand(fallbackMethod = "myFallbackMethod") 
@RequestMapping(method = RequestMethod.GET, value = "/path") 
public ResponseEntity<Resources<Resource<Data>>> getAllData() { 
    String url = "http://localhost:8080/someotherpath" 

    ParameterizedTypeReference<Iterable<Data>> responseType = new ParameterizedTypeReference<Iterable<Data>>() {}; 
    ResponseEntity<Iterable<Data>> response = restTemplate.exchange(url, HttpMethod.GET, null, responseType); 

    if (response.getStatusCode().is2xxSuccessful()) { 
     Iterable<Data> data = response.getBody(); 
     Resources<Resource<Data>> resources = assembler.toResource(data); 
     return new ResponseEntity<>(resources, response.getHeaders(), response.getStatusCode()); 
    } 
    return new ResponseEntity<>(response.getHeaders(), response.getStatusCode()); 
} 

,現在包裝數據對象轉換爲資源對象我的彙編類拋出這個錯誤錯誤消失,一切正常。

我的彙編程序類僅實現Springs的ResourceAssembler<T, D>接口並覆蓋toResource方法。

有什麼辦法可以解決這個問題嗎?

回答

1

我不知道我是否可以認爲這是一個答案,它更像是一個變通辦法:

@RequestMapping(method = RequestMethod.GET, value = "/path") 
public ResponseEntity<Resources<Resource<Data>>> getAllData() { 
    return getAllDataImpl() 
} 

@HystrixCommand(fallbackMethod = "myFallbackMethod") 
public ResponseEntity<Resources<Resource<Data>>> getAllDataImpl() { 
    String url = "http://localhost:8080/someotherpath" 

    ParameterizedTypeReference<Iterable<Data>> responseType = new ParameterizedTypeReference<Iterable<Data>>() {}; 
    ResponseEntity<Iterable<Data>> response = restTemplate.exchange(url, HttpMethod.GET, null, responseType); 

    if (response.getStatusCode().is2xxSuccessful()) { 
     Iterable<Data> data = response.getBody(); 
     Resources<Resource<Data>> resources = assembler.toResource(data); 
     return new ResponseEntity<>(resources, response.getHeaders(), response.getStatusCode()); 
    } 
    return new ResponseEntity<>(response.getHeaders(), response.getStatusCode()); 
} 

我已經把實際執行HTTP請求的方法的內容(需要用另一種方法包裝成HystrixCommand)。 它以這種方式工作,但它絕對不是一個乾淨的方式。所以如果有人知道如何以更好的方式解決這個問題..

相關問題