• Spring boot 处理复杂json接收,同种类型、不同场景处理


    场景:

    • json大体格式一致,但是 ext_info 扩展字段对象,场景不同字段不同
    • 根据某字段类型,不同值,对应不同实现的 Component,处理不同场景
    • 这里根据 event,来做不同处理
    {
        "data": {
            "event": "eventWoMan",
            "event_id": "123",
            "ext_info": {
                "dist_sex": "女",
                "dist_height": "162"
            },
            "target_name": "美女类型json"
        },
        "seq": 0
    }
    
    
    {
        "data": {
            "event": "eventWoMan",
            "event_id": "123",
            "ext_info": {
                "dist_name": "小树",
                "dist_age": "18"
            },
            "target_name": "帅哥类型json"
        },
        "seq": 0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    接收类

    @lombok.Data
    public class BusinessMsg<T> {
    
        private T data;
        private int seq;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    @lombok.Data
    public class MsgData<T> {
    
        private String event;
        @JsonProperty("event_id")
        private String eventId;
        @JsonProperty("ext_info")
        private T extInfo;
        @JsonProperty("target_name")
        private String targetName;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.Data;
    
    @Data
    public class ExtInfoMan {
    
        /**
         * 目标姓名
         */
        @JsonProperty("dist_name")
        private String distName;
    
        /**
         * 目标年龄
         */
        @JsonProperty("dist_age")
        private String distAge;
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    @Data
    public class ExtInfoWoMan {
    
    
        /**
         * 目标性别
         */
        @JsonProperty("dist_sex")
        private String distSex;
    
        /**
         * 目标高度
         */
        @JsonProperty("dist_height")
        private String distHeight;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    处理service

    public interface JsonTService {
    
        /**
         * 预处理消息
         * @param businessMsg
         */
         String handlerMsg(BusinessMsg<MsgData<JsonNode>> businessMsg);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    @Service("eventMan")
    public class JsonManServiceImpl implements JsonTService {
    
        @Resource
        private ObjectMapper objectMapper;
    
        @SneakyThrows
        @Override
        public String handlerMsg(BusinessMsg<MsgData<JsonNode>> businessMsg) {
            JsonNode extInfo = businessMsg.getData().getExtInfo();
            ExtInfoMan exitInfo = objectMapper.treeToValue(extInfo, ExtInfoMan.class);
            return exitInfo.toString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    @Service("eventWoMan")
    public class JsonWoManServiceImpl implements JsonTService {
    
        @Resource
        private ObjectMapper objectMapper;
    
        @SneakyThrows
        @Override
        public String handlerMsg(BusinessMsg<MsgData<JsonNode>> businessMsg) {
            JsonNode extInfo = businessMsg.getData().getExtInfo();
            ExtInfoWoMan extInfoWoMan = objectMapper.treeToValue(extInfo, ExtInfoWoMan.class);
            return extInfoWoMan.toString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    策略处理

    import com.service.JsonTService;
    import org.springframework.stereotype.Service;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    /**
     * @author xiaoshu
     */
    @Service
    public class JsonStrategyContext {
        
        private final Map<String, JsonTService> orderStrategyMap = new ConcurrentHashMap<>();
     
        public JsonStrategyContext(Map<String, JsonTService> strategyMap) {
            this.orderStrategyMap.putAll(strategyMap);
        }
     
        public JsonTService getResource(String event){
            return orderStrategyMap.get(event);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    实际接口接收:

    	@Resource
        private JsonStrategyContext jsonStrategyContext;
    
        @ApiOperation(value = "泛型处理,同格式json,扩展字段不一")
        @PostMapping("/revice")
        public String revice(@RequestBody BusinessMsg<MsgData<JsonNode>> businessMsg){
            String event = businessMsg.getData().getEvent();
            return jsonStrategyContext.getResource(event).handlerMsg(businessMsg);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    Java-Gui编程
    【数据结构】队列
    C++ 多态和虚函数详解
    魔百和CM311-1sa_ZG_S905L3A_安卓9.0_纯净线刷固件包
    目标检测YOLO实战应用案例100讲-基于改进的YOLOV5算法的垃圾分类模型
    【stacking】超详细模型融合方法(附图附代码)
    Active Directory用户登录报告
    程序员这个身份,比你想象的还值钱!
    【Java】常用的文件操作
    基于多目标优化算法的电力系统分析(Matlab代码实现)
  • 原文地址:https://blog.csdn.net/hesqlplus730/article/details/134019403