Java Spring引导返回以自己的对象为键的映射时出现问题

Java Spring引导返回以自己的对象为键的映射时出现问题,java,spring,rest,spring-boot,Java,Spring,Rest,Spring Boot,我的弹簧靴有问题。 我正在制作一个REST应用程序,我有一个返回映射(Share,Integer)的服务 共享是我写的一个类: public class Share { private String ticker; private String name; private Double value; public Share() { super(); } public Share(String ticker, String n

我的
弹簧靴有问题。
我正在制作一个REST应用程序,我有一个返回映射(Share,Integer)的服务

共享是我写的一个类:

public class Share {

    private String ticker;
    private String name;
    private Double value;

    public Share() {
        super();
    }

    public Share(String ticker, String name, Double value) {
        super();
        this.ticker = ticker;
        this.name = name;
        this.value = value;
    }

    public String getTicker() {
        return ticker;
    }

    public void setTicker(String ticker) {
        this.ticker = ticker;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

    @Override
    public int hashCode() {
       final int prime = 31;
       int result = 1;
        result = prime * result + ((ticker == null) ? 0 : ticker.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Share other = (Share) obj;
        if (ticker == null) {
            if (other.ticker != null)
                return false;
            } else if (!ticker.equals(other.ticker))
                return false;
        return true;
    } 

    @Override
    public String toString() {
        return "Share [ticker=" + ticker + ", name=" + name + ", value=" + value + "]";
    }
}
@RestController是:

public class ShareController {

    @Autowired
    private ShareBussines shareBussines;

    @RequestMapping("/getShare/{ticker}")
    public Share getShare(@PathVariable("ticker") String ticker) throws BrokerNotFoundException, BrokerArgumentException, BrokerGeneralException {
        return shareBussines.getShare(ticker);
    }

    @RequestMapping(value="/buyShares", method=RequestMethod.POST)
    public Map<Share, Integer> buyShares(@RequestBody Map<String,Double> sharesToBuy) throws BrokerGeneralException, BrokerArgumentException, BrokerInsufficientStockException {
        return shareBussines.buyShares(sharesToBuy);
    }

}
映射键是
share.toString()
。。。我希望密钥是共享JSON。 我试图从Share类中删除toString方法,但结果是:

{
    "Share@1eb87f": 1,
    "Share@40d9fab": 1,
    "Share@8db": 54
}
它正在使用对象的toString()


谢谢您的建议。

首先,它按照您的编码工作:

@RequestMapping(value="/buyShares", method=RequestMethod.POST)
public Map<Share, Integer> buyShares(@RequestBody Map<String,Double> sharesToBuy) throws BrokerGeneralException, BrokerArgumentException, BrokerInsufficientStockException {
    return shareBussines.buyShares(sharesToBuy);
}
然后再稍微改变一下你的服务:

@RequestMapping(value="/buyShares", method=RequestMethod.POST)
public List<ShareResponse>  buyShares(@RequestBody Map<String,Double> sharesToBuy) throws BrokerGeneralException, BrokerArgumentException, BrokerInsufficientStockException {
    // do your business here, create a list of ShareResponse and return it
    return shareBussines.buyShares(sharesToBuy); // instead of this
}
@RequestMapping(value=“/buyShares”,method=RequestMethod.POST)
public List buyShares(@RequestBody Map sharesToBuy)抛出BrokerGeneralException、BrokerArgumentException、BrokerSufficientStockException{
//在这里开展业务,创建ShareResponse列表并返回
return sharebusines.buyShares(sharesToBuy);//而不是这个
}
您应该得到一个有效的、格式良好的JSON。如果您需要每个项目都可以通过一些唯一的值进行识别,只需在
ShareResponse
中添加一些ID字段即可


这有意义吗?

这是不可能的,因为这不是一个有效的JSON。那么,您知道返回此信息的最佳方式是什么吗?我想要表示购买的股份数量。您正在使用一个非不变对象作为地图的键。。这可能会给您带来一些麻烦。如果您尝试使用
@JsonSerialize(keyussing=***)
@AlejandroLeone,一种方法是将键和值放入响应类中,并返回
列表而不是映射。见@john的答案。是的!!我现在就这么做!!谢谢你重申我的想法!!但我会退一套。谢谢你,伙计!!
public class ShareResponse {
    private Share share;
    private Integer someVal; // that's the one you have in your Map as a value

    // getters and setters
}
@RequestMapping(value="/buyShares", method=RequestMethod.POST)
public List<ShareResponse>  buyShares(@RequestBody Map<String,Double> sharesToBuy) throws BrokerGeneralException, BrokerArgumentException, BrokerInsufficientStockException {
    // do your business here, create a list of ShareResponse and return it
    return shareBussines.buyShares(sharesToBuy); // instead of this
}