0

I'm getting a 400 BAD REQUEST error with my POST Request when I add an array to the JSON object.

MemberDTO.java

public class MemberDTO{
private String memberId;
private String purchaseDate;
private List<ProductDTO> selectedProductModels;
...
}

ProductDTO.java

public enum ProductDTO {
  PRODUCT1("Product 1", "12.00", "50%', "500.00"),
  PRODUCT2("Product 2", "120.00", "80%', "100.00");

  private String productName;
  private String discount;
  private String retailPrice;
  private String price;
....
}

Controller.java

@RequestMapping(value = "member/purchase", method = RequestMethod.POST)
public String purchase(@RequestBody MemberDTO data){
 ...
 }

JSON

var purchaseData = {
    "memberId" : "12312",
    "purchaseDate" : "2015/02/14",
    "selectedProductModels" : [{
         "productName" : "sample1",
         "discount": "10%",
         "retailPrice": "100.00",
         "price": "400.98"
     },{
         "productName" : "sample2",
         "discount": "20%",
         "retailPrice": "1400.00",
         "price": "4300.98"
     }]
};

Ajax request

$.ajax({
        type    : "POST",
        url     : contextPath + "/member/purchase.do",
        dataType: "json",
        contentType:"application/json",
        data    : JSON.stringify(purchaseData),
        success : function(data) {}
});

I tried removing the "selectedProductModels" array list from json and I got successful response. Am I missing anything?

charmae
  • 1,080
  • 14
  • 38
  • I'm sure if you look at the logs you will see an exception about not being able to create the enum. Use a custom deserializer or don't use an enum at all. It's an ugly looking enum anyway. And unless the company never offers any different products and different prices and discounts, you're better off just using a class. – Paul Samsotha Jul 04 '15 at 11:52
  • @peeskillet i tried using other class instead of using that enum, i still get the same error when using an array. No exceptions. :( – charmae Jul 04 '15 at 11:54
  • Do you have the basic necessities for deserialization, no-arg constructor and proper setters? – Paul Samsotha Jul 04 '15 at 11:56

1 Answers1

0

Please try to refer the Enumeration de-serialization options discussed here

Jackson enum Serializing and DeSerializer

Serializing enums with Jackson

Converting JSON to Enum type with @RequestBody

You can specify the exact values "Product 1", "12.00", "50%", "500.00" specified in your Enum constants becuase only those values can be parsed, otherwise you have to write a custom JSON de-serializer

Note: "Product 1", "12.00", "50%', "500.00" there is a single quote in your text is that a type error?

Community
  • 1
  • 1
Dickens A S
  • 3,824
  • 2
  • 22
  • 45