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

2
  • 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. Commented Jun 25, 2015 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. Commented Jun 25, 2015 at 23:14

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

Comments

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?

9 Comments

I'm using Jersey @Danielson
I'm uncertain but I think the above also works for Jersey
The @JsonProperty is for Jackson
The GET,POST annotation are for Jersey and in POJO the @JsonProperty is Jackson. Im using both jersey and jackson for my application
@RequestBody ProductIds productIds
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.