2

I would like to iterate Products and get the list of name,code and price and set in my Model class. Any help would be really appreciated - how can I iterate this. When I use obj.get("Products") - it just printing as string - got stuck to iterate.

{
    "id": "skd3303ll333",
    "Products": [{
            "name": "apple",
            "code": "iphone-393",
            "price": "1939"

        },
        {
            "name": "ipad",
            "code": "ipad-3939",
            "price": "900"

        }
    ]
}


@PostMapping(path="/create", consumes=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> create(@RequestBody  Map<String, Object> obj ) { 
System.out.println("Products :" + obj.get("Products"));
  }
2
  • why don't you map this response directly into model using jackson Commented Nov 15, 2018 at 20:54
  • 1
    How to do that? Sorry I'm new to Spring and Java - if its very basic question Commented Nov 15, 2018 at 20:56

3 Answers 3

3

There are two ways to do this,

1) By type casting (personally i will not prefer this)

List<Map<Object,Object>> productslist = (List<Map<Object, Object>>) obj.get("products");
    for(Map entry: productslist) {
        for(Object s: entry.keySet()) {
            System.out.println(s.toString());
            System.out.println(entry.get(s).toString());

        }
    }

2) Mapping directly to Model class, for this approach you need Jackson library in buildpath

@JsonIgnoreProperties(unknown =true)
public class Customer {

@JsonProperty("id")
private String id;
@JsonProperty("products")
private List<Products> products;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public List<Products> getProducts() {
    return products;
}

public void setProducts(List<Products> products) {
    this.products = products;
   }

}
 @JsonIgnoreProperties(unknown =true)
 class Products{
@JsonProperty("name")
private String name;
@JsonProperty("code")
private String code;
@JsonProperty("price")
private String price;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
   }

}

Controller

public ResponseEntity<Object> create(@RequestBody  Customer obj ) {
Sign up to request clarification or add additional context in comments.

Comments

2

You need POJO structure with two classes:

public class Product {
    private String name;
    private String code;
    private int price;
}

public class ProductsGroup {
    private long id;
    private List<Product> products;
    // getters/setters
}

And change your method signature to:

@PostMapping(path="/create", consumes=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ProductsGroup> create(@RequestBody ProductsGroup productGroup) 
{ 
   System.out.println("Products :" + productGroup.getProducts());
}

Comments

1

You are trying to process the json using a Map<String, Object> obj, which could be possible in some way, but mostly what you want to do is define a single or multiple POJO classes. These represent the json.

public class IdWrapper {
    private String id;
    @JsonProperty("Products")
    private List<Product> products;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Product> getProducts() {
        return products;
    }
}

public class Product {
    private String name;
    private String code;
    private String price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

And in you controller like this:

@RestController
@RequestMapping("test")
public class DemoController {
    @PostMapping()
    public void test(@RequestBody IdWrapper productsWrapper) {
        System.out.println();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.