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