副标题:Java如何从HttpServletRequest中读取HTTP请求的body
今天接触一个项目,前台用angular4 post访问后台,
this.httpService.post({url: 'quality/IMSI_MO', IMSImsg: this.InputMsg, TIME1: time1, TIME2: time2 }).subscribe(res => {this.dialing = res; });
public post(msg): Observable<any[]> {return this.http.post(this.domain + [msg.url],JSON.stringify(msg), {headers: this.headers}).map(res => res.json() as any[]); }
后台用servlet接收参数。
发现用request.getParameter获取不到参数。
String imsi =request.getParameter("search_imsi");
发现imsi值是null,获取不到。而以往的JQuery ajax能获取到。
之前用Springmvc 能获取到angular4 post的值
public @ResponseBody List<Map<String, Object>> getLTEmesctime(@RequestBodyMap<String, String> map) {........}
查看angular post的报文和非angularpost的报文
报文主体部分传递的不同。angular传递了json,json字符串这个整体又没有参数对应。所以request.getParameter是获取不到的。
只能读取主体的json字符串内容,然后转成Map对象,从Map对象中获取响应的值。
Gson gson=new Gson();Map<String,String> resultMap=new HashMap<String,String>();BufferedReader br = request.getReader();String str, wholeStr = "";while ((str = br.readLine()) != null) {wholeStr += str;}System.out.println(wholeStr);if(!"".equals(wholeStr)) {Map<String, String> map =gson.fromJson(wholeStr,Map.class);String imsi =map.get("search_imsi");String msisdn =map.get("search_msisdn");String p_hour_start =map.get("search_p_hour_start");String p_hour_end =map.get("search_p_hour_end");System.out.println("Searchimsi传入search_imsi:" + imsi);System.out.println("Searchimsi传入search_msisdn:" + msisdn);System.out.println("Searchimsi传入search_p_hour_start:" + p_hour_start);System.out.println("Searchimsi传入search_p_hour_end:" + p_hour_end);
|