springboot如何返回中文json,保证顺序。LinkedHashMap应用实例

在业务中有时候需要中文json去进行映射到有些UI上,而springboot都是英文字段

java 复制代码
    //通过id查询消火栓的基本信息和检测值给POI
    @GetMapping("/queryPOIForHydrant")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "查询所需要的消火栓数据渲染给POI", notes = "传入id")
    public R queryPOIForHydrant(@RequestParam(value = "id", required = true) String id) {

        //先查询阀门的基本信息
        QueryWrapper<HydrantInformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HydrantInformationEntity::getHydrantId, id);
        HydrantInformationEntity detail = hydrantInformationService.getOne(queryWrapper);
        Map<String, Object> resultMap =  new LinkedHashMap<>();;
        if (detail != null) {
            resultMap.put("消火栓编号", detail.getHydrantId());
            resultMap.put("地址", detail.getRegion());
            // 根据实际Entity属性继续添加转换逻辑
            // 注意:这里的中文字段和实体类的属性需要根据实际情况对应修改
        }
        return R.data(resultMap);

    }

希望在转换成JSON时保持Map中元素的顺序,可以考虑使用LinkedHashMap代替HashMapLinkedHashMap内部维护了一个双向链表,可以保持元素插入的顺序。这意味着当您遍历Map或将其转换成JSON时,元素的迭代顺序将会是它们插入顺序。

相关推荐
柒和远方9 分钟前
从审计日志到可下载证据包:事务型 Outbox、租约 fencing 与保留水位
前端·后端·架构
卓怡学长15 分钟前
w266基于spring boot + vue 圣地延安美食乐享系统
java·数据库·vue.js·spring boot·spring·intellij-idea
geovindu1 小时前
CSharp: Prototype Pattern
开发语言·后端·设计模式·.net·原型模式·创建型模式
Listen·Rain2 小时前
Springboot整合Ollama实现调用本地多模型
java·spring boot·后端
张三丰23 小时前
Agent 不只是聊天框:用 ArkUI 做一个鸿蒙任务工作台
后端
卷无止境3 小时前
从Python的cryptography库出发,从零开始理解密码学,到构建零信任网络
后端·python
Augustzero3 小时前
`co_await` 按下暂停键之后:从零看懂 C++20 协程
c++·后端
宠友信息3 小时前
多端即时通讯源码技术实践,Redis路由、MySQL持久化与Socket接入
spring boot·websocket·mysql·uni-app
Tom·Ge3 小时前
【Spring Boot 4】Spring Boot 4.0 AI Agent实战:Skills Agent + MCP协议集成
人工智能·spring boot·microsoft
Devin~Y3 小时前
电商场景下的Java面试实战:从Spring Boot微服务到Kafka、Redis与AI RAG
java·spring boot·redis·elasticsearch·spring cloud·微服务·kafka