SpringMVC的介面,接收json資料返回json資料並且解析為List物件集合
阿新 • • 發佈:2019-02-04
請求引數實體類
package com.lifuyi.entity;
/**
* 請求引數**重點內容**
*/
public class RequestPram {
//訂單號
private String orderNum;
//缸號
private String batchNum;
//該缸號的具體生產進度
private String node;
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this .orderNum = orderNum;
}
public String getBatchNum() {
return batchNum;
}
public void setBatchNum(String batchNum) {
this.batchNum = batchNum;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
@Override
public String toString() {
return "RequestPram [orderNum=" + orderNum + ", batchNum=" + batchNum + ", node=" + node + "]";
}
}
響應引數實體類
package com.lifuyi.entity;
/**
* 響應引數
*/
public class ResponsePram {
private String resultCode;
private String desc;
public ResponsePram () {
super();
}
public ResponsePram( String desc,String resultCode) {
super();
this.desc = desc;
this.resultCode = resultCode;
}
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "ResponsePrams [resultCode=" + resultCode + ", desc=" + desc + "]";
}
}
控制器
package com.lifuyi.process;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lifuyi.entity.RequestPram;
import com.lifuyi.entity.ResponsePram;
@Controller
@RequestMapping("/flow")
public class ProcessController {
/**
* Erp系統請求路徑 http://localhost/lifuyi/flow/detail.do
* @return 返回Json資料{result:{resultCode:xxx,desc:xxx}}
* 請求路徑:http://localhost/lifuyi/flow/detail.do
* 請求引數:{"allFlows":[{"orderNum":"20180109170317448001","batchNum":"test","node":"001"},{"orderNum":"20180109170317448005","batchNum":"測試","node":"001"}] }
*/
@RequestMapping(value="/detail",method=RequestMethod.POST, produces="application/json;charset=UTF-8")
@ResponseBody
public String erpRequest(@RequestBody String parms) {
// System.out.println(parms);
JSONObject jsonObject = JSONObject.parseObject(parms); //將str轉化為相應的JSONObject物件
System.out.println(jsonObject);
String str = jsonObject.getString("allFlows"); //取出allFlows對應的值,值為字串
//使用JSONArray.parseArray(String, Class<T>)將字串轉為指定物件集合
List<RequestPram> listPram = (List<RequestPram>) JSONArray.parseArray(str, RequestPram.class);
for (RequestPram requestPram : listPram) {
System.out.println(requestPram.toString());
}
//http://localhost/lifuyi/flow/detail.do {"allFlows":[{"orderNum":"20180109170317448001","batchNum":"test","node":"001"}] }
Map<String,ResponsePram> map = new HashMap<String,ResponsePram>();
ResponsePram rp = new ResponsePram("001", "測試中文");
map.put("result", rp);
String json = JSON.toJSONString(map);
System.out.println(json);
return json;
}
}
測試