我对 Spring Boot
有疑问。
我正在制作一个 REST 应用程序,并且我有一个返回 Map(Share, Integer) 的服务
Share是我写的一个类:
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
}
它正在使用对象的 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);
}
分享是这里的关键。这有点奇怪。为什么不创建一些像这样的对象:
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
}
你应该得到一个有效的、“格式化”的 JSON。如果您需要每个项目都可以通过某个唯一值进行识别,只需将一些 ID 字段添加到 ShareResponse
。
这有什么意义吗?)
关于java - Spring Boot 以自己的对象作为键返回 Map 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788164/