400 - 错误的请求 - 请求在语法上是不正确的

时间:2016-07-11 18:52:17

标签: java angularjs rest http

这是我的代码

var myObj = 
{
   "id": 0,
   "createdDate": "12-12-2014 12:00:00",
   "fromEmail": "abc@gmail.com",
   "sampleBooleanValue": false,
   "extraDescrition":"ssfsvgsf",
   "sampleArraay":[{"arrayElem1"}, {"arrayElem2"}]
};

console.log(downtime1);

$rootScope.httpPost('createMyObj/', myObj).success(function (successdata) {
    console.log(successdata);
}).error(function (errordata) {
    console.log(errordata);
});

我使用URI createMyObj创建了我的REST端点,但是当我点击提交时,我得到了400珠请求 - 提交的请求在语法上是不正确的错误。

我的JSON格式是否正确?

编辑: 这是我的相关Java bean

public class MyObj { @Id private int id; private String fonEmail; @ElementCollection private List<String> sampleArraay; private ZonedDateTime createdDate; private Boolean sampleBooleanValue; private String extraDescription;

2 个答案:

答案 0 :(得分:2)

Your array from the sampleArraay field is invalid. Try:

var myObj = {
  "id": 0,
  "createdDate": "12-12-2014 12:00:00",
  "fromEmail": "abc@gmail.com",
  "sampleBooleanValue": false,
  "extraDescrition":"ssfsvgsf",
  "sampleArraay":["arrayElem1", "arrayElem2"]
};

console.log(downtime1);
$rootScope.httpPost('createMyObj/', myObj).success(function (successdata) {
  console.log(successdata);
})
.error(function (errordata) {
  console.log(errordata);
});

答案 1 :(得分:0)

"sampleArraay":[{"arrayElem1"}, {"arrayElem2"}]

Looks to be wrong. Were you planning for the elements of sampleArraay to be nested objects?

Also at the risk of being flippant, the spelling in your example, words like "Array" and "Description" are wrong. Could it be a case of being spelled wrong in one place and not in the other?

One thing I like to do when I get 400 errors like this is progressively simplify the object I am trying to send by commenting out elements until I get to the culprit.

I did this on JSFiddle.com (a great resource) with your code and a simple alert statement to confirm the array problem.

相关问题