总结angular + springboot http发送请求的常用方法。
最近做的项目用到了angular + springboot 技术栈。刚入门,所以想总结一下简单的技术要点。
angular 怎么发送post、get请求。 springboot 怎么接受get、post、delete请求。
- 先回忆下springboot有几种发送请求的方式。
Get ,有三种常见的接受请求方式,分别是PathVariable、requestParam、requestBody:
@GetMappring('/findById/{id}') public String findUserById(@PathVariable String Id){ return id; } @GetMappring("/findUserlist") public List<User> findUserList(@RequestParam String uid , @RequestParam String name){ return new ArrayList<User>; } @GetMapping("/findUserList") public List<User> getUser(@RequestBody UserReq usr){ return new ArrayList<User>; }
Post 和 Delete接受请求的方式也是跟Get一样,所以就不写了。
另外想说的是,原来@RequestBody有3种+方式来接收实体类参数的。
分别是用:Entity、JsonNode、List<String,Map<String,Object>> 来接收//第一种 @PostMapping("/search") public List<User> search(@RequestBody User user){ return new ArrayList<User>; } //第二种 @PostMapping("/search") public List<User> search(@RequestBody JsonNode requestNode){ return new ArrayList<User>; } //第三种 @PostMapping("/search") public List<User> search(@RequestBody List<Map<String,Object>>){ return new ArrayList<User>; }
2.angular怎么发送get、post请求。
发送Get请求,参数在URL后
findUserById(uid: string) : Observable{
return this.http.get(${this.apiUrl}${this.aprFindByid}${uid}) // 用${uid}这种传递。
}
发送Get请求,参数在URL上面
findUserByCondition(uid:string,name:string): Observable<User []>{
return this.http.get<Uset []>(this.apiUrl + this.apiFindUser + ‘?uid=’+uid + ‘&name=’+name );
}
POST请求Body请求。
findUserByCondition(userId: string,username:string,role: string): Observable {
let params = {
‘userId’:userId,
‘username’: username,
‘role’: roleName
}
return this.http.post(this.apiUrl + this.apiEnquiry,params, {headers: new HttpHeaders({
‘Content-Type’: ‘application/json’,
‘Access-Control-Allow-Origin’: ‘*’
});});
}
3万+

被折叠的 条评论
为什么被折叠?



