通过发布请求,spring boot 和 angularjs 之间的错误请求错误

Posted

技术标签:

【中文标题】通过发布请求,spring boot 和 angularjs 之间的错误请求错误【英文标题】:Bad request errror between spring boot and angularjs by a post request 【发布时间】:2016-11-20 03:02:47 【问题描述】:

我有两个小项目,后端 spring-boot 端负责提供数据,angularjs 前端部分只是显示数据。我正在尝试从角度侧发布一个 json,而 spring-boot 侧消耗并发送响应。

这是 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
                       ];

它基本上是一个数组,但是 *** 上的某个人告诉 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;

赛马模型:

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

【问题讨论】:

HorseRaceModel代码也放上。 试试这个@RequestBody HashMap&lt;String, List&lt;HorseRaceModel&gt;&gt; data @SSH 仍然出现同样的错误 这是我从 java 控制台得到的:嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无法反序列化 java.util.HashMap 的实例出 START_ARRAY 令牌 【参考方案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;
    

最后,应该指示消费帖子的方法将请求正文解析为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)); 打印的对象指针。您可能想要打印其他内容。您可能还想为包装对象和模型使用更好的名称(而不是 listListModel

评论注释:您的HorseRaceModel 类没有空构造函数,您需要提供一个才能使反序列化工作

【讨论】:

遇到类似错误:无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:没有找到适合类型 [simple type, class ganyan.HorseRaceModel] 的构造函数:无法从 JSON 对象实例化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?) 添加了一个空的构造函数,现在它可以工作了,谢谢! @brainmassage 欢迎!我将编辑添加需要一个空的构造函数

以上是关于通过发布请求,spring boot 和 angularjs 之间的错误请求错误的主要内容,如果未能解决你的问题,请参考以下文章

通过 Axios 发送一个 post 请求是在 Spring-Boot 后端生成一个空的 RequestBody。在 Postman 中工作,但不能通过 Axios 发布请求

Node.js/AngularJS 前端调用 Spring Boot API 后端

Spring Boot Security OAuth Angular 对预检请求的响应没有通过?

Spring-Boot:同时处理多个请求

Spring Boot 休息服务 |不支持请求方法“GET”

配置 Spring Boot 以侦听 localhost 旁边的请求