2016-11-22 77 views
0

我试图发布包含一个id(int)和一个数组对象,但我得到从服务器端一个HTTP 400错误的请求响应。这是我迄今为止...

的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: [email protected]; 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: [email protected]; line: 1, column: 28] 
(through reference chain: com.blue.beans.GuardarVentaRequest["venta"]- 
>java.util.ArrayList[0]) 

Chrome's Network Tab Output

任何想法?

+0

在浏览器的网络选项卡(右键单击>检查>网络),你应该得到什么地方出了错你的要求更多信息。 – lucasnadalutti

+0

你用PostMan测试过你的服务吗? – hurricane

+0

我打算使用它,但是你在代码中看到任何奇怪的东西吗? –

回答

0

试试这3件事。

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

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

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

  3. 尝试使用angular.toJson

希望帮助序列化请求负载!

+0

是的!用户teppic刚刚告诉我同样的事情......这就是解决方案,谢谢你们! –

+0

太棒了!此外,如果该数组受范围限制并被更新,则FYI有时倾向于添加'$$ hashset'键。因此,使用angular.toJson序列化请求对象是一种很好的做法。 – Nilsaw