如何以角度获取请求?

时间:2019-05-28 14:08:39

标签: angular spring

我需要从Angular到Spring Java的GET请求,但出现错误 415

zone.js:3243 GET http://localhost:8080/user/friend/1 415

这是我在Spring Java中到端点的代码

 @RequestMapping(
            value = "/friend/{idUser}",
            method = RequestMethod.GET,
            consumes = "application/json",
            produces = "application/json")
    public ResponseEntity<List<Friendship>> getFriend(@PathVariable Long idUser) {
        return new ResponseEntity<List<Friendship>>(userServiceImpl.getFriends(idUser), HttpStatus.OK);
    }

这是我的角度代码,用于从后端请求数据

getFriends(idUser) {
        console.log('UNA', idUser);
        const config = new HttpHeaders().set('Content-Type', 'application/json');
        const url = 'http://localhost:8080/user/friend/' + idUser;
        const body = JSON.stringify({"idOwner": idUser});
        return this.http.get<Object[]>(url);
    }

1 个答案:

答案 0 :(得分:1)

您在http get中缺少配置,因此您的代码应为

return this.http.get<Object[]>(url, config);