0

This is the json i'm sending via POST request

{
  "PIds" : [ "MOB123", "ELEC456"]
}

This is my class that receives the JSON,

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/GetProductsInfo")
public List<ProductDetails> getProductsInfo(ProductIds productIds) {

    System.out.println(productIds + "   ");

    DBCursor<ProductDetails> dbCursor = collection.find(DBQuery.in("pid", productIds.getPIds()));

    List<ProductDetails> products = new ArrayList<>();
    while (dbCursor.hasNext()) {
        ProductDetails product = dbCursor.next();
        products.add(product);
    }
    return products;
}

Im converting the JSON array into the 'ProductIds' object, this is my POJO class

public class ProductIds
{
    @JsonProperty("PIds")
    private List<String> pIds;

    public List<String> getPIds()
    {
        return pIds;
    }

    public void setPIds(List<String> Pids)
    {
        this.pIds = Pids;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [PIds = "+ pIds +"]";
    }
}

The problem for me here is that the JSON is not getting populated into the java object 'productIds' is NULL, I dont know why. Im new to Jackson can someone help me out. Thank you

Ferooz Khan
  • 93
  • 3
  • 11
  • Which REST library do you use? If Jersey what is your Jersey config? Have you registered Jackson feature? BTW, you may use List directly as argument for getProductsInfo. – Dmytro Jun 25 '15 at 12:16
  • You could take advantage of the `GET` verb and remove the word from your path: `GET /ProductsInfo`. Then `POST` could be reused on the same URI to create/update. – Sam Berry Jun 25 '15 at 23:14

2 Answers2

1

Did you try to get the ID's out using .getJSONArray()? Uses the org.json library.

JSONObject obj = new JSONObject(jsoninput);

JSONArray jsonArray = obj.getJSONArray("PIds");
Camelaria
  • 284
  • 6
  • 23
0

Are you using JAXB? Jersey? fasterxml.jackson? I'm getting the impression you are using several at the same time

RestClass:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/GetProductsInfo")
public List<ProductDetails> getProductsInfo(@RequestBody ProductIds productIds) {
   // etc
}

For JAXB:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ProductIds {
    @XmlElement("PIds")
    private List<String> pIds;

    public List<String> getPIds() {
        return pIds;
    }

    public void setPIds(List<String> Pids) {
        this.pIds = Pids;
    }

    @Override
    public String toString() {
        return "ClassPojo [PIds = " + pIds + "]";
    }
}

Otherwise take a look at:

Convert a JSON string to object in Java ME?

How to convert the following json string to java object?

Community
  • 1
  • 1
Danielson
  • 2,605
  • 2
  • 28
  • 51