Angular中的My Post方法
private attachAuthorization(): RequestOptions {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('HttpHeaders.ACCEPT', 'MediaType.APPLICATION_JSON_VALUE');
headers.append('Content-Type', 'charset=UTF-8');
headers.append ('Authorization',sessionStorage.getItem('token'));
//console.log(localStorage.getItem('token'));
const options = new RequestOptions({
headers: headers,
responseType: ResponseContentType.Json,
});
return options;}
public post(url: string, requestData: any):any {
const options = this.attachAuthorization();
return this.http.post(url, JSON.stringify(requestData),options);
}
Json数组
[{ “COLUMNNAME”: “符号”, “filterby”: “”, “值”: “”, “时间”: “”, “数据类型”: “字符串”},{ “COLUMNNAME”:“order_Receiving_Date “ ”filterby“: ”“, ”值“: ”“, ”时间“: ”“, ”数据类型“: ”DATERANGE“},{ ”COLUMNNAME“: ”EVENT_TYPE“, ”filterby“: ”“,” 值“:””, “时间”: “”, “数据类型”: “字符串”},{ “COLUMNNAME”: “firm_ROE_ID”, “filterby”: “”, “值”: “”, “时间”: “” “数据类型”: “INT”},{ “COLUMNNAME”: “rejected_ROE_ID”, “filterby”: “”, “值”: “”, “时间”: “”, “数据类型”: “INT”}] < / p>
API方法
@RequestMapping(value = {"/Oats-Exception-summary"}, method = RequestMethod.POST)
public ResponseEntity<List<OatsExceptionSummary>> OatsExceptionSummaryPost(
@RequestBody JSONArray payload)throws SQLException,JSONException,Exception {
System.out.println(payload);
String FilterData="";
/*JSONObject jsonObj=new JSONObject(payload);*/
List<OatsExceptionSummary> Data =ISurveillanceService.getOatsExecptionSummary(FilterData);
if (Data.isEmpty()) {
return new ResponseEntity<List<OatsExceptionSummary>>(HttpStatus.NO_CONTENT);
} else {
return new ResponseEntity<List<OatsExceptionSummary>>(Data, HttpStatus.OK);
}
}
** API错误
无法读取HTTP消息: org.springframework.http.converter.HttpMessageNotReadableException: 缺少必需的请求正文:public org.springframework.http.ResponseEntity&GT; com.zcon.RSMS.SpringBoot_RSMS2.controller.SurveillanceController.OatsExceptionSummaryPost(org.json.JSONArray) 投 java.sql.SQLException中,org.json.JSONException,java.lang.Exception的 2018-05-22 12:56:09.624 WARN 11855 --- [nio-8090-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver:已解决的异常导致 由Handler执行: org.springframework.http.converter.HttpMessageNotReadableException: 缺少必需的请求正文:public org.springframework.http.ResponseEntity&GT; com.zcon.RSMS.SpringBoot_RSMS2.controller.SurveillanceController.OatsExceptionSummaryPost(org.json.JSONArray) 投 java.sql.SQLException中,org.json.JSONException,java.lang.Exception的
答案 0 :(得分:1)
解决了 ..问题是我已经发送空数组reauestData []但是现在 我已将数组更改为json对象
{
“数据”: [
{ “COLUMNNAME”: “符号”, “filterby”: “”, “值”: “”, “时间”: “”, “数据类型”: “字符串”},{ “COLUMNNAME”: “order_Receiving_Date” “filterby”: “”, “值”: “”, “时间”: “”, “数据类型”: “DATERANGE”},{ “COLUMNNAME”: “EVENT_TYPE”, “filterby”: “”, “值” : “”, “时间”: “”, “数据类型”: “字符串”},{ “COLUMNNAME”: “firm_ROE_ID”, “filterby”: “”, “值”: “”, “时间”: “”, “数据类型”: “INT”},{ “COLUMNNAME”: “rejected_ROE_ID”, “filterby”: “”, “值”: “”, “时间”: “”, “数据类型”: “INT”}
}
发布方法
component.ts
prepareData() {
console.log("this.FilterData"+JSON.stringify(this.FilterData));
this.loading = true;
this.SharedHttpClientService.post(
this.UrlsService.setAPIURl(
APIURL.Surveillance_OatsException_Summary),
this.FilterData)
.map((response: Response) => {
this.isLoadingResults = false;
this.isRateLimitReached = false;
return response.json();
})
.subscribe(Element => {
this.dataset=Element;
},
(err: HttpErrorResponse) => {
this.isLoadingResults = false;
this.isRateLimitReached = true;
});
this.loading = false;
}
service.ts
private attachAuthorization(): RequestOptions {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
//headers.append('HttpHeaders.ACCEPT', 'MediaType.APPLICATION_JSON_VALUE');
headers.append ('Authorization',sessionStorage.getItem('token'));
//console.log(localStorage.getItem('token'));
const options = new RequestOptions({
headers: headers,
responseType: ResponseContentType.Json,
});
return options;
}
public post(url: string, requestData: any):Observable<any>{
const options = this.attachAuthorization();
//console.log(localStorage.getItem('token'));
let Data={"data":requestData}
console.log("Data "+JSON.stringify(Data));
return this.http.post(url,JSON.stringify(Data),options);
}
API控制器功能
@RequestMapping(value = {"/Oats-Exception-summary/"}, method = RequestMethod.POST)
public ResponseEntity<List<OatsExceptionSummary>> OatsExceptionSummaryPost(
@RequestBody Map payload)throws SQLException,JSONException,Exception {
JSONObject root = new JSONObject( payload);
JSONArray dataArray = root.getJSONArray("data");
for (int t=0; t<dataArray.length(); t++) {
JSONObject JObject = dataArray.getJSONObject(t);
System.out.println(JObject.getString("columnname"));
}
String FilterData="";
//JSONObject jsonObj=new JSONObject(payload);
List<OatsExceptionSummary> Data =ISurveillanceService.getOatsExecptionSummary(FilterData);
if (Data.isEmpty()) {
return new ResponseEntity<List<OatsExceptionSummary>>(HttpStatus.NO_CONTENT);
} else {
return new ResponseEntity<List<OatsExceptionSummary>>(Data, HttpStatus.OK);
}
}
答案 1 :(得分:0)
//fist convert your json array into typescript
// than assin to any variable like
documents: [{
columnname: string;
filterby: string;
values: string;
time: string;
datatype: string;
}];
//than pass to in service like below
this.profileService.saveProfile(this.documents)
.subscribe(response => { });
//than use in your http post service like below example
saveProfile(formData): Observable<any> {
return this.http.post(this.requestUrl, formData)
.map((res: Response) => {return res.json()})
.catch(this.handleError);
}
答案 2 :(得分:0)
当您的API需要JSON数组时,您正在对帖子正文进行字符串化。只需删除JSON.stringify
,然后重试。
return this.http.post(url, requestData, options);