• 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
  • 相关阅读:
    Redis高可用集群(哨兵、集群)
    显示命令行控制台(cmd.exe)的几种方式——Qt、C++
    【golang】调度系列之sysmon
    局部放电发生的现象特点以及解决方案
    spring-security源码学习总结
    黑盒测试方法论—边界值
    ECU的软硬件架构
    docker应用部署---nginx部署的配置
    Linux常用命令— 挂载命令--U盘挂载
    Servlet中乱码解决
  • 原文地址:https://blog.csdn.net/hesqlplus730/article/details/134019403