2016-11-22 5 views
0

Ich versuche, ein Objekt, das eine ID (Int) und ein Array enthält, aber ich bekomme eine http 400 Bad Request Response von der Server-Seite. Dies ist, was ich bisher ...

Java Bean-Objekt für Anfrage:

public class GuardarVentaRequest { 

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

Java-Objekt:

public class Venta { 

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

Java Controller:

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

AngularJS Service:

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); 
    } 
    ); 
} 

Array:

$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); 

Ausgang:

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

Irgendwelche Ideen?

+0

In der Registerkarte Ihres Browsers (Rechtsklick> inspect> Netzwerk) sollten Sie mehr Informationen darüber erhalten, was bei Ihrer Anfrage schief gelaufen ist. – lucasnadalutti

+0

Haben Sie Ihren Service mit PostMan getestet? – hurricane

+0

Ich benutze es, aber siehst du irgendwas komisches im Code? –

Antwort

0

Versuchen Sie diese 3 Dinge.

  1. Fügen Sie GuardarVentaRequest und Venta den Standardkonstruktor hinzu.

    GuardarVentaRequest() {} & Venta() {}

  2. Überprüfen Sie, ob ein HTTPMessageConverter wurde Ihrer Feder Config hinzugefügt. ZB: MappingJackson2MessageConverter (Überprüfen Sie die Kompatibilität mit Ihrer Federversion).

  3. versuchen, mithilfe angular.toJson

Hoffnung, die hilft, die Anfrage-Payload Serialisierung!

+0

Ja! Die User-Teppic haben mir das gleiche gesagt ... Das war die Lösung, danke euch beiden! –

+0

Großartig! Auch FYI, manchmal eckig, neigt dazu, einen '$$ -Hashset'-Schlüssel hinzuzufügen, wenn das Array durch den Bereich begrenzt ist und aktualisiert wird. Es ist also eine gute Vorgehensweise, angle.toJson zu verwenden, um das Anforderungsobjekt zu serialisieren. – Nilsaw