后台返回Object/Map类型,前端接收到的类型也会不同

2018-06-24  本文已影响479人  墨色尘埃

1、后台返回类型是Object,前端android接收到的也是Object


image.png

后台接口

/**
     * 获取所有的图层信息
     */
    @RequestMapping(value = "/selectLayerList", method = RequestMethod.GET)
    public ResponseObj<LayerInfoConfig> selectLayerList(@RequestParam String userId) {
//    public ResponseObj<Map<String, List>> selectLayerList(@RequestParam String userId) {
        try {
            /**获取部门名称*/
            List<LinkedHashMap<String, Object>> userInfoMap = layerMapper.superManagerSelect("SELECT * FROM t_user " +
                    "where userId = '" + userId + "'");
            String department = String.valueOf(userInfoMap.get(0).get("department"));
            List<LayerConfig> layerList = layerMapper.selectLayerList(userId, department);

            /**构造图层字段配置信息字典*/
            List<LayerFieldInfo> allLayerFieldInfo = layerMapper.getAllLayerFieldInfo();
            Map<String, List<LayerFieldInfo>> layerFieldInfoMap = new HashMap<>();
            for (LayerFieldInfo layerFieldInfo : allLayerFieldInfo) {
                if (!layerFieldInfoMap.containsKey(layerFieldInfo.getLayerId())) {
                    layerFieldInfoMap.put(layerFieldInfo.getLayerId(), new ArrayList<LayerFieldInfo>());
                }
                layerFieldInfoMap.get(layerFieldInfo.getLayerId()).add(layerFieldInfo);
            }

            /***将图层字段信息列表放入各图层中*/
            for (LayerConfig layerConfig : layerList) {
                if (layerFieldInfoMap.containsKey(layerConfig.getLayerId())) {
                    layerConfig.setFieldInfoList(layerFieldInfoMap.get(layerConfig.getLayerId()));
                }
            }

            List<DicType> dic = layerMapper.getDic();

            /**返回Map类型**/
            Map<String, List> map = new HashMap<>();
            map.put("layerList", layerList);
            map.put("dicType", dic);

            /**返回Object类型**/
            LayerInfoConfig layerInfoConfig = new LayerInfoConfig();
            layerInfoConfig.setLayerList(layerList);
            layerInfoConfig.setDicType(dic);

            return new ResponseObj<>(layerInfoConfig, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseObj<>(null, new Error(-1, "服务器请求异常"));
        }

    }

前端

    /**
     * 获取图层
     *
     * @return
     */
    @GET("layer/selectLayerList")
    Observable<ResponseObj<LayerInfoConfig>> getLayerInfo(@Query("userId") String userId);
    /**
     * 从服务器获取图层信息列表
     */
    public void GetLayerInfoFromServer() {
        ApiManager.getInstance()
                .pointApiService()
                .getLayerInfo(LoginManager.getUserId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(listResponseObj -> {
                    layerConfigList = listResponseObj.data.getLayerList();
                    Log.e("LayerConfig", "图层获取成功:" + layerConfigList.size() + " ");
                    UpdateLayerConfigInfo(layerConfigList);
                }, throwable -> {
                    Toast.makeText(IApplication.mInstance, throwable.getMessage(), Toast.LENGTH_LONG);
                    Log.e("LayerConfig", "图层未获取成功");
                });
    }

2、后台返回类型是Map,前端android接收到的也是LinkedTreeMap


image.png

后台

    /**
     * 获取所有的图层信息
     */
    @RequestMapping(value = "/selectLayerList", method = RequestMethod.GET)
//    public ResponseObj<LayerInfoConfig> selectLayerList(@RequestParam String userId) {
    public ResponseObj<Map<String, List>> selectLayerList(@RequestParam String userId) {
        try {
            /**获取部门名称*/
            List<LinkedHashMap<String, Object>> userInfoMap = layerMapper.superManagerSelect("SELECT * FROM t_user " +
                    "where userId = '" + userId + "'");
            String department = String.valueOf(userInfoMap.get(0).get("department"));
            List<LayerConfig> layerList = layerMapper.selectLayerList(userId, department);

            /**构造图层字段配置信息字典*/
            List<LayerFieldInfo> allLayerFieldInfo = layerMapper.getAllLayerFieldInfo();
            Map<String, List<LayerFieldInfo>> layerFieldInfoMap = new HashMap<>();
            for (LayerFieldInfo layerFieldInfo : allLayerFieldInfo) {
                if (!layerFieldInfoMap.containsKey(layerFieldInfo.getLayerId())) {
                    layerFieldInfoMap.put(layerFieldInfo.getLayerId(), new ArrayList<LayerFieldInfo>());
                }
                layerFieldInfoMap.get(layerFieldInfo.getLayerId()).add(layerFieldInfo);
            }

            /***将图层字段信息列表放入各图层中*/
            for (LayerConfig layerConfig : layerList) {
                if (layerFieldInfoMap.containsKey(layerConfig.getLayerId())) {
                    layerConfig.setFieldInfoList(layerFieldInfoMap.get(layerConfig.getLayerId()));
                }
            }

            List<DicType> dic = layerMapper.getDic();

            /**返回Map类型**/
            Map<String, List> map = new HashMap<>();
            map.put("layerList", layerList);
            map.put("dicType", dic);

            /**返回Object类型**/
            LayerInfoConfig layerInfoConfig = new LayerInfoConfig();
            layerInfoConfig.setLayerList(layerList);
            layerInfoConfig.setDicType(dic);

            return new ResponseObj<>(map, null);
//            return new ResponseObj<>(layerInfoConfig, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseObj<>(null, new Error(-1, "服务器请求异常"));
        }

    }

前端

    /**
     * 获取图层
     *
     * @return
     */
    @GET("layer/selectLayerList")
    Observable<ResponseObj<Map<String, List>>> getLayerInfo(@Query("userId") String userId);
    /**
     * 从服务器获取图层信息列表
     */
    public void GetLayerInfoFromServer() {
        ApiManager.getInstance()
                .pointApiService()
                .getLayerInfo(LoginManager.getUserId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(listResponseObj -> {
//                    layerConfigList = listResponseObj.data.getLayerList();
                    layerConfigList = listResponseObj.data.get("layerList");
                    Log.e("LayerConfig", "图层获取成功:" + layerConfigList.size() + " ");
                    UpdateLayerConfigInfo(layerConfigList);
                }, throwable -> {
                    Toast.makeText(IApplication.mInstance, throwable.getMessage(), Toast.LENGTH_LONG);
                    Log.e("LayerConfig", "图层未获取成功");
                });
    }

后台:E:\IDEAProject\ideasrfaserver,LayerController类P51行
前端:E:\ASProject\newapp,LayerStore类P249行GetLayerInfoFromServer()方法

上一篇下一篇

猜你喜欢

热点阅读