簡體   English   中英

AngularJS和Spring MVC-$ http 400錯誤請求

[英]AngularJS and Spring MVC - $http 400 Bad Request

我正在嘗試發布一個包含id(int)和數組的對象,但是我從服務器端收到了HTTP 400 Bad請求響應。 這是我到目前為止所擁有的...

請求的Java Bean對象:

public class GuardarVentaRequest {

private Integer idCliente;
private List<Venta> venta; ... (Getters and Setters code)

Java對象:

public class Venta {

private Integer id;
private String nombre;
private Integer precio;
private Integer cantidad;
private Integer total; ... (Getters and Setters code)

Java控制器:

@RequestMapping(value = "/guardarVenta", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void venta(@RequestBody GuardarVentaRequest factura){
    System.out.println(factura);
}

AngularJS服務:

function guardarVenta(array){                                       
    let factura = {
        idCliente : parseInt($('#cliente').val()),
        venta : array,
    };
    console.log(factura);
    $http({
        method: 'POST',
        url: '/blue/guardarVenta',
        contentType: 'application/json',
        data: factura
    }).then(
    function successCallback(response){
        console.log(response.statusText);
    },
    function errorCallback(response){
        console.log(response.statusText);
    }
    );
}

陣:

$scope.productos = new Array();
let productoInfo = {
        id: $scope.producto.id,
        nombre: $scope.producto.nombre,
        precio: $scope.producto.precio,
        cantidad: $scope.cantidadProducto,
        total: $scope.producto.precio * $scope.cantidadProducto 
    }
    $scope.productos.push(productoInfo);

輸出:

ADVERTENCIA: Failed to read HTTP message: 
org.springframework.http.converter.HttpMessageNotReadableException: Could 
not read document: Can not construct instance of com.blue.beans.Venta: no 
suitable constructor found, can not deserialize from Object value (missing 
default constructor or creator, or perhaps need to add/enable type 
information?)
at [Source: java.io.PushbackInputStream@663359f3; line: 1, column: 28] 
(through reference chain: com.blue.beans.GuardarVentaRequest["venta"]-
>java.util.ArrayList[0]); nested exception is 
com.fasterxml.jackson.databind.JsonMappingException: Can not construct 
instance of com.blue.beans.Venta: no suitable constructor found, can not 
deserialize from Object value (missing default constructor or creator, or 
perhaps need to add/enable type information?)
at [Source: java.io.PushbackInputStream@663359f3; line: 1, column: 28] 
(through reference chain: com.blue.beans.GuardarVentaRequest["venta"]-
>java.util.ArrayList[0])

Chrome的“網絡”標簽輸出

有任何想法嗎?

試試這三件事。

  1. 將默認構造函數添加到GuardarVentaRequest和Venta。

    GuardarVentaRequest(){}和Venta(){}

  2. 檢查是否已將HTTPMessageConverter添加到您的spring配置中。 例如: MappingJackson2MessageConverter (檢查與您的Spring版本的兼容性)。

  3. 嘗試使用angular.toJson序列化請求有效負載

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM