您的位置:首页 > 编程语言 > Java开发

SpringMVC的接口,接收json数据返回json数据并且解析为List对象集合

2018-01-30 14:19 1176 查看
请求参数实体类

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;
}
}


测试





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐