1

I was reading a lot but I don't know what is the error in my code. I hope you can help me. Regards.

Model: RenglonFacturaDTO(With his getters/setters)

public class RenglonFacturaDTO implements Comparable<RenglonFacturaDTO>{

   private Integer idfact;
   private Long idrenglon;
   private Integer idremito;
   private Float cant;
   private String descripadic;
   private Double precio;
   private Double subtotal;
}

This is the list of objects that i send via ajax to controller

<script>
   $(document).ready(function(){
    
       var cliente = $('.id').data('id');
       var token = $("meta[name='_csrf']").attr("content");
       var header = $("meta[name='_csrf_header']").attr("content"); 
       var listado = [{}];
      

      $('#generar').click(function(){

         cliente = $('#idcliente').data('id');
    
         $('.renglon').each(function(){
        
             elem = $(this);    
        
             cant = elem.find('.cant').val();
             precio = elem.find('.precio').val();
             descripadic = elem.find('.descripadic').val();
             idremito = elem.find('.idremito').text();
             idrenglon = elem.data('idrenglon');
             idfact = $('#factura').data('idfact');
             subtotal = cant * precio;
        
             listado.push({"cant": parseFloat(cant),"precio": precio ,"descripadic":descripadic,"subtotal":subtotal ,"idremito":idremito ,"idrenglon":idrenglon , "idfact":idfact});
        
         });

       $.ajax({
          "type":"post",
          "url":"agregarRenglonesFactura",
          "dataType": 'json',
          "cache": false,
          "data": {"listado": listado},
          beforeSend: function(xhr) {
            // here it is
            xhr.setRequestHeader(header, token);
          }
        
         });
     });
 });
</script>

Controller

@RequestMapping(value="/agregarRenglonesFactura", method= RequestMethod.POST) 
public String redAgregarRenglonFactura(@ModelAttribute ArrayList<RenglonFacturaDTO> renglonesfacturadto) throws Exception{
    
        Iterator<RenglonFacturaDTO> it = renglonesfacturadto.iterator();
        
        while(it.hasNext()){
        
            RenglonFacturaDTO renglonfacturadto = it.next();
        
        System.out.println("***************AGREGAR RENGLON*******************IDRENGLONREMITO: " + renglonfacturadto.getIdrenglon() + " CANTIDAD: " + renglonfacturadto.getCant());
        }
        
        return ("TAMAÑO LISTA: " + renglonesfacturadto.size());
}   

this is the error:

 Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: 
Invalid property 'listado[10][cant]' of bean class [ar.edu.grupoesfera.cursospring.modelo.ListadoRenglonesFacturaDTO]:
Property referenced in indexed property path 'listado[10][cant]' is neither an array nor a List nor a Map; returned value was [ar.edu.grupoesfera.cursospring.modelo.RenglonFacturaDTO@67e12cdf]] con causa raíz

This is the array that i send via post

listado[0][cant]15
listado[0][descripadic]
listado[0][idfact]12
listado[0][idremito]9
listado[0][idrenglon]36
listado[0][precio]135
listado[0][subtotal]2025
listado[1][cant]15
listado[1][descripadic]
listado[1][idfact]12
listado[1][idremito]9l
istado[1][idrenglon]36
listado[1][precio]135
listado[1][subtotal]2025
listado[2][cant]15
listado[2][descripadic]
listado[2][idfact]12
listado[2][idremito]9
listado[2][idrenglon]36
listado[2][precio]135
listado[2][subtotal]2025
1
  • You should create a proper JSON something like below:- []
    – Akhil
    Commented Apr 24, 2017 at 12:28

2 Answers 2

0

With JQuery ajax requests, especially if you're sending a list, you want to use JSON.stringify().

"data": JSON.stringify({ 'listado': listado }),

As a side note, you're also passing a null value with your list because of the way it's instantiated. You could just write

var listado = [];
1
  • Thank you for answer, I try this but nothing happen, it works without exceptions but the list is empty.
    – Marcos B.
    Commented Apr 24, 2017 at 11:28
0

You should create a proper JSON before posting it to controller, something like below

[ { "cant": 15, ... } ]

And your controller RequestMapping should look like below:-

@RequestMapping(value="/agregarRenglonesFactura", method= RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE) 
public String  
redAgregarRenglonFactura(@RequestBody ArrayList<RenglonFacturaDTO> 
renglonesfacturadto) throws Exception{}
4
  • If i send via post a json array, i get: The server rejected this requirement because the required entity is in a format not supported by the required resource for the required method. if also, I add "headers": { 'Content-Type': 'application/json', 'Accept': 'application/json' }, to the jquery ajax, i get the following: The requirement sent by the customer was syntactically incorrect.
    – Marcos B.
    Commented Apr 24, 2017 at 13:00
  • Change your ajax call to "data": listado
    – Akhil
    Commented Apr 24, 2017 at 13:13
  • It is the same, i get Http 415, but i change @ModelAttribute to @RequestParam("listado) List<String> and i get the following: {"listado":[{"idfact":12 "idrenglon":22 "idremito":9 "cant":1 "descripadic":"" "precio":135 "subtotal":135} {"idfact":12 "idrenglon":23 "idremito":9 "cant":2 "descripadic":"" "precio":65 "subtotal":130} {"idfact":12 "idrenglon":24 "idremito":9 "cant":3 "descripadic":"" "precio":450 "subtotal":1350} ]} but i don't know how can bind with a collection of type RenglonFacturaDTO.
    – Marcos B.
    Commented Apr 25, 2017 at 12:01
  • 415 is unsupported media type, refer this SO post stackoverflow.com/questions/32664316/…
    – Akhil
    Commented Apr 25, 2017 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.