繁体   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