JSON请求400响应:“客户端发送的请求在语法上不正确”

时间:2015-05-23 21:11:05

标签: java json

我们有这样的对象:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@Document(collection = Order.COLLECTION_NAME)
public class Order implements Serializable {
public static final String COLLECTION_NAME = "orders";

@Id
private Long id;
private Long orgId;
private Long userId;
private Date creationDate = new Date();
private Integer statusCode = Status.NEW.code();
private Integer typeCode;
private Integer paymentCode;
private Date date;
private String phoneNumber;
private List<UserAddressList> userAddressList;
private BigDecimal cash;
private Integer persons;
private String comments;
private List products;

@Indexed(unique = true)
private String token = UUID.randomUUID().toString();

private Boolean deleted = Boolean.FALSE;

在这个对象中,我们有两个列表,其中包含另外两个对象:

private List userAddressList;  
private List products;

public class UserAddressList implements Serializable {

private String street;
private String house;
private String building;
private Long flat;
private Long entrance;
private Long floor;
private String intercom;


public class ProductPack implements Serializable {
private Long productId;
private Integer quantity;

public ProductPack() {
}

public ProductPack(Long productId, Integer quantity) {
    this.productId = productId;
    this.quantity = quantity;
}

我们有这样的api方法来添加新订单:

@RequestMapping(value = "/add", method = RequestMethod.POST)
public @ResponseBody ResponseDto addOrder(
     @RequestHeader(value = "Authorization") String authorization,
     @RequestBody final Order order) {

当我们将此JSON发送到服务器时:

{"statusCode": 0,  
 "typeCode": 1,  
 "paymentCode": 4,  
 "date": 2430919020590,  
 "phoneNumber": "+79216450091",  
 "userAddressList":  
 [{  
 "street": "Test",  
 "house": "House",  
 "building": "A",  
 "entrance": 1,  
 "floor": 8,  
 "intercom": "intercom",  
 "flat": 12  
 }]  
 ,  
 "persons": 1,  
 "products":   
 [{  
 "productId": 97,  
 "quantity": 5  
 }  
 ]}  

它返回“客户端发送的请求在语法上不正确”

当我们发送这个JSON时:

{"statusCode": 0,  
"typeCode": 1,  
"paymentCode": 4,  
"date": 2430919020590,  
"phoneNumber": "+79216450091",  
"persons": 1,  
"products": [  
{  
"productId": 97,  
"quantity": 5  
}  
]}  

服务器接受它但userAddressList为空

你能帮忙解决问题吗?

1 个答案:

答案 0 :(得分:0)

检查JSON请求和代码中字段之间的一致性。您声明的字段可能不是服务器映射的字段。例如, UserAddressList 可以是地址