【发布时间】:2017-12-26 20:41:19
【问题描述】:
我使用 Apache cxf 和 spring 框架编写了一个 java REST Web 服务。我的服务正在正确生成 XML。但是当我将媒体类型从 XML 更改为 JSON 时,我得到以下输出
No message body writer has been found for class java.util.ArrayList, ContentType: application/json
我是否需要使用任何其他库,例如 Jackson(我不知道它是什么)来生成 JSON。这是我的模型类
package au.com.fxa.sfdc.custdocs.Model;
import java.math.BigDecimal;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Statement
{
private Date statementDate;
private BigDecimal totalAmount;
private String imageLocation;
private String accountNumber;
public Statement(){}
public Date getStatementDate()
{
return this.statementDate;
}
// public String getFormattedStatementDate()
// {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// return sdf.format(this.statementDate);
// }
public BigDecimal getTotalAmount()
{
return this.totalAmount;
}
public void setStatementDate(Date statementDate)
{
this.statementDate = statementDate;
}
public void setTotalAmount(BigDecimal totalAmount)
{
this.totalAmount = totalAmount;
}
public String getImageLocation()
{
return this.imageLocation;
}
public void setImageLocation(String imageLocation)
{
this.imageLocation = imageLocation;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
}
这是我的服务方法下的代码
@GET
@Path("{custid}/statements")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({"application/x-www-form-urlencoded"})
public List<Statement> getStatementsListByAccountId(
@PathParam("custid") String account,
@DefaultValue("") @QueryParam("fromdate") String fromDate,
@DefaultValue("") @QueryParam("todate") String toDate) throws Exception {
Date from = null;
Date to = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
if(!fromDate.equals(""))
{
from = formatter.parse(fromDate);
}
if(!toDate.equals(""))
{
to = formatter.parse(toDate);
}
ArrayList<Statement> statements = (ArrayList<Statement>) CustomerBiz.getStatements("Statements",account,from,to);
return statements;
}
我错过了什么??
【问题讨论】:
标签: java json web-services jax-rs