【问题标题】:AngularJS and Spring MVC - $http 400 Bad RequestAngularJS 和 Spring MVC - $http 400 错误请求
【发布时间】:2016-11-22 20:18:54
【问题描述】:

我正在尝试发布一个包含 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's Network Tab Output

有什么想法吗?

【问题讨论】:

  • 在浏览器的网络选项卡中(右键单击 > 检查 > 网络),您应该会获得有关您的请求出了什么问题的更多信息。
  • 您是否使用 PostMan 测试过您的服务?
  • 我打算用它,但是你看到代码中有什么奇怪的地方吗?
  • 它说“com.blue.beans.Venta:找不到合适的构造函数,无法从 Object 值反序列化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)”。 Venta 有默认构造函数吗?
  • 天哪!你拯救了我的一天! Venta 和 GuardarVentaRequest 类中需要构造函数...非常感谢!!!!

标签: javascript java angularjs spring spring-mvc


【解决方案1】:

试试这 3 件事。

  1. 为 GuardarVentaRequest 和 Venta 添加默认构造函数。

    GuardarVentaRequest(){} & 文塔(){}

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

  3. 尝试使用angular.toJson序列化请求负载

希望有帮助!

【讨论】:

  • 是的!用户 teppic 刚刚告诉我同样的事情......这就是解决方案,谢谢你们!
  • 太棒了!此外,仅供参考,如果数组受范围限制并被更新,有时角度倾向于添加一个 '$$hashset' 键。因此使用 angular.toJson 序列化请求对象是一种很好的做法。
猜你喜欢
  • 2021-09-24
  • 2017-12-20
  • 2016-04-20
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
相关资源
最近更新 更多