Spring Boot返回Map的问题以自己的对象为关键

时间:2018-06-10 21:22:49

标签: java spring rest spring-boot

我对Spring Boot有疑问。 我正在创建一个REST应用程序,我有一个返回Map(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);
    }

}

问题是当我从Postman调用该服务时。

结果是:

{
    "Share [ticker=AMZN, name=Amazon, value=259.32126508258295]": 1,
    "Share [ticker=GOOGL, name=Google, value=249.35339337497606]": 1,
    "Share [ticker=FB, name=Facebook, value=181.15005639608364]": 55
}

Map键是share.toString() ...我希望键是共享JSON。 我尝试从Share类中删除toString方法,但结果是:

{
    "Share@1eb87f": 1,
    "Share@40d9fab": 1,
    "Share@8db": 54
}

它正在使用Object的toString()。

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

首先,它在您编码工作时起作用:

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

分享是关键所在。这有点奇怪。为什么不创建一些像这样的对象:

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
}

你应该得到一个有效的,很好的格式化的&#39; JSON。如果您需要通过某些唯一值识别每个项目,只需在ShareResponse添加一些ID字段。

它有意义吗?)