You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot接收外部Map参数异常及复杂Key场景咨询

Spring Boot中自定义对象作为Map Key的参数接收问题

我在Spring Boot项目中编写了一个Controller,其中接收List<Product>参数的方法可正常运行,但接收Map<UniqueID, Product>参数的方法无法成功接收请求。

Controller代码

@RestController
public class MyBoot1Controller {

    @PostMapping(value = "/storeList")
    public String storeProducts(@RequestBody List<Product> products) {
        System.out.println("Received Products count: " + products.size());
        System.out.println(products);
        return "received " + products.size() + " products into the List.";
    }

    @PostMapping(value = "/storeMap")
    public String storeProducts(@RequestBody Map<UniqueID, Product> products) {
        System.out.println("Received Products count: " + products.size());
        System.out.println(products);
        return "received " + products.size() + " products into the Map.";
    }
}

实体类定义

Product类

public class Product {
    int pid;
    String pname;
    String ptype;
    int plife;
    //getters & setters
}

初始UniqueID类

final public class UniqueID {
    public String uid;

    public UniqueID(String uid) {
        super();
        this.uid = uid;
    }

    @Override
    public String toString() {
        return uid;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    @Override
    public boolean equals(Object u) {
        UniqueID ud = (UniqueID) u;
        return (ud.uid == uid);
    }

    @Override
    public int hashCode() {
        int h = 0, i;
        for (i = 0; i < uid.length(); i++)
            h += (i + 1) * uid.charAt(i);
        h += uid.length();
        return h;
    }
}

测试用例

正常工作的List类型JSON入参

[
    {
        "pid": 1,
        "pname": "laptop",
        "ptype": "computer",
        "plife": 10
    },
    {
        "pid": 2,
        "pname": "keyboard",
        "ptype": "computer",
        "plife": 12
    },
    {
        "pid": 3,
        "pname": "Plastic Flowers",
        "ptype": "Decoration",
        "plife": 3
    }
]

无法生效的Map类型JSON入参(尝试用对象作为Key)

{   
    {
        "uid":"first"
    }:
    {
        "pid": 1,
        "pname": "laptop",
        "ptype": "computer",
        "plife": 10
    },

    {
        "uid":"second"
    }:
    {
        "pid": 2,
        "pname": "keyboard",
        "ptype": "computer",
        "plife": 12
    },


    {
        "uid":"third"
    }:
    {
        "pid": 3,
        "pname": "Plastic Flowers",
        "ptype": "Decoration",
        "plife": 3
    }
}

可正常工作的Map类型JSON入参(String作为Key)

{
  "first": {
    "pid": 1,
    "pname": "laptop",
    "ptype": "elec",
    "plife": 10
  },
  "second": {
    "pid": 2,
    "pname": "phone",
    "ptype": "elec",
    "plife": 10
  },
  "third": {
    "pid": 3,
    "pname": "purse",
    "ptype": "leather",
    "plife": 20
  },
  "fourth": {
    "pid": 4,
    "pname": "book",
    "ptype": "stationery",
    "plife": 100
  }
}

疑问

当UniqueID包含多个字段时,该如何正确传递Map参数?

final public class UniqueID {
    public String uid;
    public String personalNumber;
    public String citizenshipNumber;
}

内容的提问来源于stack exchange,提问作者PoornaChandra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 14:20:21