2017-08-14 137 views
0

我使用Spring Boot來創建需要在Angular 4中使用的API。Spring和Angular在不同的端口上。Spring + Angular:如何解析ResponseEntity的角度?

問題是Spring的ResponseEntity在Angular中引發了一個錯誤。

@RequestMapping(value = "/{id}", method = RequestMethod.GET) 
    public ResponseEntity getFlow(@PathVariable int id) { 
     Flow flow = flowService.findById(id); 

     return new ResponseEntity(flow, HttpStatus.FOUND); 
    } 

現在,我可以完美地使用Postman來測試API,它的工作原理。 enter image description here

但是,當我提出一個要求,從角,它返回一個錯誤: enter image description here

enter image description here

奇怪的是,它返回一個錯誤的請求的對象旁邊。

現在,問題的原因是Spring Boot應用程序返回一個ResponseEntity而不是一個普通的對象(如String),Angular不知道如何解釋它。如果控制器只返回一個Flow對象,它就可以工作。

如何使用ResponseEntity解決問題?或者,我還可以如何將對象與HTTP狀態碼一起發送?

+1

有什麼具體的原因爲什麼HttpStatus.Found被使用?這是一個成功的迴應嘗試發送HttpStatus.Ok – Barath

回答

0

https://docs.angularjs.org/api/ng/service/ $ HTTP

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also, status codes less than -1 are normalized to zero. -1 usually means the request was aborted, e.g. using a config.timeout. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the outcome (success or error) will be determined by the final response status code.

當您發送ResponseEntity的一個實例(HttpStatus.Found),其在這裏提到的文件HTTP狀態代碼是302,它的成功範圍下犯規秋天這就是爲什麼調用錯誤回調。