-1

I have written a java REST web service using Apache cxf and spring framework. My service is correctly producing XML. But when I change from Media type from XML to JSON I get following output

No message body writer has been found for class java.util.ArrayList, ContentType: application/json

Do I need to use any other library such as Jackson ( I don't know what it is ) to produce JSON. Here is my model class

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;
    }

}

Here is the code under my service method

@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;
    }

What am I missing ??

Adeel
  • 413
  • 1
  • 7
  • 22
  • suppose you refer this example [post](https://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/) – Rajith Pemabandu Jul 20 '17 at 23:52
  • That means do I need to use Jersey now instead of CXF ? or just use jersey-json dependency ? – Adeel Jul 20 '17 at 23:53
  • 1
    suppose `cxf` is kind of out dated now. if you still want to use `Apache cxf` refer this [post](https://examples.javacodegeeks.com/enterprise-java/rest/creating-jax-rs-web-service-using-apache-cxf/) on returning a JSON – Rajith Pemabandu Jul 20 '17 at 23:58
  • @RajithPemabandu Thanks. that's what I was looking for. – Adeel Jul 21 '17 at 00:27

1 Answers1

0

The easiest way is to wrap your list in a container, as suggested here. Example:

@XmlRootElement
public class Statements {

    private List<Statement> statements;

    public List<Statement> setStatements(List<Statement> statements) {
        this.statements = statements;
    }

    @XmlElement
    public List<Statement> getStatements() {
        return statements;
    }
}

Further information: Jackson is a JSON processing library, meaning that id is able to parse JSON and perform data binding, among other things. There seem to be other ways of getting this to work by using Jackson, according to this post, avoiding the use of a wrapper.

José Belo
  • 139
  • 1
  • 8