The payload you receive will be a JSON string. So, you could define a POJO class to package the data and then deserialize to the appropriate object and use it. Serilize and deserialize the data as event body between a POJO and JSON string by using a json library. You can choose any library you want from here: http://www.json.org/.
This is the code to receive :
public class Product {
public Product(){
}
private String product;
private Double price;
public Product(String json ){
Gson gson=new Gson();
try{
Product product =gson.fromJson(json, Product.class);
if (product!=null){
System.out.println("Name: " +product.getProduct());
}
}catch (Exception e){
System.out.println("failed: " +e.getMessage());
}
}
public String getProduct() {
return product;
}
public Double getPrice() {
return price;
}
}
And, this is how you can read the JSON String into a Object from the Class Product. Hence, the Product class will contain variables with product and price!
public Product(String json ){
Gson gson=new Gson();
try{
Product product =gson.fromJson(json, Product.class);
if (product!=null){
System.out.println("Name: " +product.getProduct());
}
}catch (Exception e){
System.out.println("failed: " +e.getMessage());
}
}