SpringMVC的接口,接收json數據返回json數據並且解析為List對象集合


請求參數實體類

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

測試

Postman測試
控制台結果
測試結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM