如何从angular发出get请求,以便在spring引导控制器中传递@RequestBody



我正在开发一个角度弹簧引导应用程序。我的目标是在我的spring控制器中接收@RequestBody参数。阅读互联网上的帖子,我只找到了发送@RequestBody params的POST请求,而在我的情况下,这是一个GET请求。然而,我坚持使用POST控制器方法。这是我的控制器

@RequestMapping(value = "/produitimmobilier/all/{pageSize}/{page}",
method = RequestMethod.POST,
produces = {"text/plain;charset=UTF-8", MediaType.APPLICATION_JSON_VALUE},
consumes = {"text/plain;charset=UTF-8", MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<ProduitImmobilierDTO> findAll(@PathVariable("pageSize") int pageSize, @PathVariable("page") int page, @RequestBody Search search){
logger.info(search.toString());
return produitImmobilierService.findAll(pageSize, page);
}

我试图在角度方面包含一个帖子请求,如下面的

getListProduitImmobilierDTO(pagesize: number, page: number, search: Search): Observable<ProduitImmobilierDTO[]> {
const headerDict = {
'Content-Type': 'application/json',
Accept: 'application/json',
'Accept-Charset': 'charset=UTF-8',
'Access-Control-Allow-Headers': 'Content-Type'
};
const requestOptions = {
headers: new HttpHeaders(headerDict)
};
return this.http.post<Search>('/api/produitimmobilier/all/' + pagesize + '/' + page, search, requestOptions).pipe(map((jsonArray: any) =>jsonArray.map((jsonItem) => ProduitImmobilierDTO.fromJson(jsonItem))));
}

我得到了这个错误

Error:  occured while trying to proxy to: localhost:4200/api/produitimmobilier/all/5/1
Message: Http failure response for http://localhost:4200/api/produitimmobilier/all/5/1: 504 Gateway Timeout

据我所知,我不能把帖子<搜索>具有搜索对象参数并且接收Observable<ProduitImmobileDTO[]>回答我在网上什么也没找到。你能帮我吗?

也许我误解了这个问题,可能是错的,但您正在向应用程序的角度地址处的服务器发出请求。这可以在错误文本localhost:4200/api中看到。请尝试联系服务器工作地址。例如:

return this.http.post<Search>('http:localhost:8080/api/produitimmobilier/all/' + pagesize + '/' + page, search, requestOptions).pipe(map((jsonArray: any) =>jsonArray.map((jsonItem) => ProduitImmobilierDTO.fromJson(jsonItem))));

您需要插入服务器的地址,而不是http://localhost:8080

最新更新