这是我目前遇到的问题。我的DAO类返回索引中的数据,但我想以JSON格式将所选数据返回给客户端。我该怎么办?
以json格式返回数据的控制器类。
MyController.java
@RequestMapping(value = "/ohlc",method = RequestMethod.POST)
public @ResponseBody List<OhlcResponse> getOhlc(@RequestBody OhlcRequest ohlcRequest) {
List<OhlcResponse> ohlc = ohlcService.getOhlc(ohlcRequest);
return ohlc;
}
Dao类通过执行存储过程返回4个数据(minprice,maxprice,closingprice和previousclosingprice)。
OhlcDaoImpl.java
public List<OhlcResponse> getOhlc(OhlcRequest ohlcRequest) {
session = sessionFactory.openSession();
SQLQuery q = session.createSQLQuery("EXEC uspGetOhlc :StockCode, :fromDate, :toDate");
q.setString("StockCode",ohlcRequest.getStockSymbol());
q.setDate("fromDate",ohlcRequest.getFromDate());
q.setDate("toDate", ohlcRequest.getToDate());
List<OhlcResponse> l = q.list();
return l;
}
我的存储过程
USE WealthFeedSrv
GO
CREATE PROCEDURE uspGetOhlc
@StockCode varchar(50),
@fromDate date,
@toDate date
AS
BEGIN
SELECT spd.ClosingPrice, spd.PreviousClosingPrice, spd.MinPrice, spd.MaxPrice
FROM StockPriceDetl spd
inner join Stock stk on stk.Id = spd.StockId
inner join StockPriceMast spm on spm.Id = spd.MastId
WHERE stk.StockSymbol= @StockCode AND spm.TranDate Between @fromDate and @toDate
END
GO
我想要的pojo类返回绑定的数据。
OhlcResponse.java
public class OhlcResponse {
private BigDecimal MaxPrice;
private BigDecimal MinPrice;
private BigDecimal PreviousClosingPrice;
private BigDecimal ClosingPrice;
public BigDecimal getMaxPrice() {
return MaxPrice;
}
public void setMaxPrice(BigDecimal maxPrice) {
MaxPrice = maxPrice;
}
public BigDecimal getMinPrice() {
return MinPrice;
}
public void setMinPrice(BigDecimal minPrice) {
MinPrice = minPrice;
}
public BigDecimal getPreviousClosingPrice() {
return PreviousClosingPrice;
}
public void setPreviousClosingPrice(BigDecimal previousClosingPrice) {
PreviousClosingPrice = previousClosingPrice;
}
public BigDecimal getClosingPrice() {
return ClosingPrice;
}
public void setClosingPrice(BigDecimal closingPrice) {
ClosingPrice = closingPrice;
}
public OhlcResponse(){
}
}
我想要的结果
"maxPrice":"200",
"minPrice":"300",
"ClosingPrice":"400",
"PreviousClosingPrice":"500"
错误说无法转换为OhlcResponse。如何将OhlcDao返回的对象绑定到OhlcResponse类并返回JSON格式?