我有两个小项目,后端spring-boot端负责提供数据,而angularjs前端部分只是显示那些数据。我试图从有角度的方面张贴一个json,并且弹簧引导方消耗并发送响应。
这是json的赛马数据; “jokey”表示骑手,包含骑手信息:
var list = {"horses": [{"horseId":45942,"horseName":"Decolte","raceId":8449,"kilo":61.0,"jokey":{"name":"AYETULLAH DEMİR","raceNumber":41,"first":1,"second":4,"third":0,"fourth":1},"ekuriId":0},
{"horseId":27520,"horseName":"Busıness Man","raceId":8449,"kilo":57.0,"jokey":{"name":"CİVAN ÖZŞAHİN","raceNumber":190,"first":7,"second":15,"third":18,"fourth":12},"ekuriId":0},
{"horseId":55856,"horseName":"Erselçuk","raceId":8449,"kilo":57.0,"jokey":{"name":"NAİL EREN","raceNumber":64,"first":2,"second":0,"third":4,"fourth":2},"ekuriId":0},
{"horseId":52940,"horseName":"Haşim Ağa","raceId":8449,"kilo":57.0,"jokey":{"name":"DOĞUKAN AYDOĞAN","raceNumber":380,"first":11,"second":18,"third":10,"fourth":24},"ekuriId":0},
{"horseId":53431,"horseName":"İhtiyar","raceId":8449,"kilo":57.0,"jokey":{"name":"CÜNEYİT GÖKÇE","raceNumber":598,"first":32,"second":52,"third":64,"fourth":65},"ekuriId":0},
{"horseId":51778,"horseName":"Urla Zamanı","raceId":8449,"kilo":57.0,"jokey":{"name":"ADEM ŞEN","raceNumber":280,"first":18,"second":25,"third":32,"fourth":32},"ekuriId":0},
{"horseId":51816,"horseName":"Wın Every Day","raceId":8449,"kilo":57.0,"jokey":{"name":"EMRE NALBANT","raceNumber":405,"first":19,"second":26,"third":36,"fourth":33},"ekuriId":0},
{"horseId":58650,"horseName":"Lıon Amed","raceId":8449,"kilo":52.0,"jokey":{"name":"CANER KARADEMİR","raceNumber":134,"first":7,"second":7,"third":8,"fourth":7},"ekuriId":0},
{"horseId":51239,"horseName":"Catch The Wınd","raceId":8449,"kilo":57.0,"jokey":{"name":"MÜSLÜM CANPOLAT","raceNumber":238,"first":5,"second":12,"third":12,"fourth":19},"ekuriId":0},
{"horseId":46263,"horseName":"Ian Tapp","raceId":8449,"kilo":58.0,"jokey":{"name":"ERDEM NUR TÜFEKÇİ","raceNumber":79,"first":3,"second":1,"third":4,"fourth":5},"ekuriId":0},
{"horseId":51647,"horseName":"Sılverado","raceId":8449,"kilo":57.0,"jokey":{"name":"ÜMİT DERYA ALTEKİN","raceNumber":1185,"first":48,"second":53,"third":64,"fourth":84},"ekuriId":0},
{"horseId":57231,"horseName":"Junıor Duru","raceId":8449,"kilo":58.0,"jokey":{"name":"BEDRİ TEPE","raceNumber":716,"first":45,"second":55,"third":50,"fourth":67},"ekuriId":0}
]};
它基本上是一个数组但是stackoverflow上的某个人告诉ajax请求中的数据应该是一个对象,所以我在前面添加了“horses:”。 json是在代码中手动添加的。
这是http请求:
$http({
url: 'http://localhost:8080/horseHistory',
method: 'POST',
contentType: "application/json",
data: list.horses,
headers: {
"Content-Type": "application/json"
}
}).success(function(data) {
console.log(data);
});
在后端我只想看到一个有效的http连接,所以它基本上是空的。这是spring-boot功能:
@RequestMapping(value = "/horseHistory", method = RequestMethod.POST )
public ResponseEntity<Void> getHorseHistory(@RequestBody HorseRaceModel[] horseRaces) throws IOException {
System.out.println(horseRaces[0]);
return null;
}
HorseRaceModel:
package ganyan;
public class HorseRaceModel {
int horseId;
String horseName;
int raceId;
double kilo;
JokeyModel jokey;
int ekuriId;
public HorseRaceModel(int horseId, String horseName, int raceId, double kilo, JokeyModel jokey, int ekuriId) {
this.horseId = horseId;
this.horseName = horseName;
this.raceId = raceId;
this.kilo = kilo;
this.jokey = jokey;
this.ekuriId = ekuriId;
}
public int getHorseId() {
return horseId;
}
public void setHorseId(int horseId) {
this.horseId = horseId;
}
public void setHorseName(String horseName) {
this.horseName = horseName;
}
public String getHorseName() {
return horseName;
}
public int getRaceId() {
return raceId;
}
public void setRaceId(int raceId) {
this.raceId = raceId;
}
public double getKilo() {
return kilo;
}
public void setKilo(double kilo) {
this.kilo = kilo;
}
public JokeyModel getJokey() {
return jokey;
}
public void setJokey(JokeyModel jokey) {
this.jokey = jokey;
}
public int getEkuriId() {
return ekuriId;
}
public void setEkuriId(int ekuriId) {
this.ekuriId = ekuriId;
}
}
JokeyModel:
public class JokeyModel {
private String name;
private int raceNumber;
private int first;
private int second;
private int third;
private int fourth;
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRaceNumber() {
return raceNumber;
}
public void setRaceNumber(int raceNumber) {
this.raceNumber = raceNumber;
}
public int getFirst() {
return first;
}
public void setFirst(int first) {
this.first = first;
}
public int getThird() {
return third;
}
public void setThird(int third) {
this.third = third;
}
public int getFourth() {
return fourth;
}
public void setFourth(int fourth) {
this.fourth = fourth;
}
}
Chrome控制台出错:
Failed to load resource: the server responded with a status of 400 (Bad Request) http://localhost:8080/horseHistory
来自Java控制台的错误:
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token
答案 0 :(得分:1)
我不熟悉Spring Boot,我不知道为什么你被告知要发布一个JSON对象而不是JSON数组,但我发现你并没有连贯地做到这一点。
您将JSON数组包装在名为list
的JSON对象中,但通过指定data: list.horses
,您仍然只传递嵌入的JSON数组。由于您要发送整个JSON对象,因此应指定data: list
。
此外,我认为您需要在服务器端定义包装器模型,这可能是这样的类:
public class ListModel {
private List<HorseRaceModel> horses;
public List<HorseRaceModel> getHorses() {
return horses;
}
public void setHorses(List<HorseRaceModel> horses) {
this.horses = horses;
}
}
最后,应该指示使用post的方法将请求主体解析为ListModel
。这意味着将方法修改为:
@RequestMapping(value = "/horseHistory", method = RequestMethod.POST )
public ResponseEntity<Void> getHorseHistory(@RequestBody ListModel horseRaces) throws IOException {
System.out.println(horseRaces.getHorses().get(0).getName());
return null;
}
旁注:我还修改了代码以打印第一匹马的名称,而不是System.out.println(horseRaces.getHorses().get(0));
打印的对象指针。您可能想要打印其他内容。您可能还希望为包装器对象和模型使用更好的名称(而不是list
和ListModel
)
注释注释:您的HorseRaceModel
类没有空构造函数,您需要提供一个反序列化才能工作