zoukankan      html  css  js  c++  java
  • 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测试
    控制台结果
    测试结果

    勿忘初心 得过且过
  • 相关阅读:
    jQuery 事件注册
    jQuery 获取元素当前位置offset()与position()
    jquery scrollTop()与scrollLeft()
    linux常用命令
    php5.6+apache2.4环境配置
    php 开启socket配置
    Node.js的学习路线
    apache配置rewrite及.htaccess文件(转载)
    php 获取域名等Url
    html5的本地存储
  • 原文地址:https://www.cnblogs.com/xpf1009/p/9227287.html
Copyright © 2011-2022 走看看