1

I'm trying to retrieve a list of javascript objects in Java Spring MVC but when it arrives to the controller is empty.

I've been reading a lot about but I don't know how it works

This is my ajax code:

$.ajax({
                    url : 'ajax/abrir_cotizacion',
                    data : {
                        listaunidades: [{
                                idtipounidad: 1, 
                                modelo: 2013, 
                                cantidad: 1, 
                                tipopago: 1,
                                costounidad: 1500000,
                                ivaoperacion: 25000,
                                totaloperacion: 1504000,
                                enganche: 750000,
                                idplazo: 3
                            },
                            {
                                idtipounidad: 2, 
                                modelo: 2012, 
                                cantidad: 2, 
                                tipopago: 2,
                                costounidad: 1500000,
                                ivaoperacion: 25000,
                                totaloperacion: 1504000,
                                enganche: 750000,
                                idplazo: 6
                            }]
                    },
                    type : 'POST',
                    dataType : 'json',
                    success : function(data) {

                    },
                    error : function(xhr, status) {
                        alert('Disculpe, existió un problema');
                    },
                    complete : function(xhr, status) {
                        //alert('Petición realizada');
                    }
                });

And below my controller and model required to this.

@ResponseBody
@RequestMapping(value = "/ajax/abrir_cotizacion", method = RequestMethod.POST)
public Object abrircotizacion(Model model, HttpServletRequest request, @ModelAttribute ArrayList<Unidades> listaunidades) {
    try {
        Injector inj = AppInjector.getInjector();
        return new MsgPojo(1, "Se abre la cotización");
    } catch (Exception ex) {
        LoggerUtils.printLog(this.getClass(), Level.SEVERE, ex, null, Thread.currentThread().getStackTrace());
        return new MsgPojo(-1, "Ocurrio un error al cargar los datos. " + ex.toString());
    }
}


public class Unidad {

    private int idtipounidad;
    private int modelo;
    private int cantidad;
    private int tipopago;
    private double costounidad;
    private double ivaoperacion;
    private double totaloperacion;
    private double enganche;
    private int idplazo;

   //getters and setters
}

1 Answer 1

1

i'm using Spring Boot 2.1.4
and i have tried this and it is working.

HomeController.class

@RestController
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @PostMapping("/test")
    public void test(@RequestBody UserDTO[] userDTO) {
        logger.info(userDTO.length + ""); //expect 2
    }

}

UserDTO.class

@Data
public class UserDTO {

    private String email;
    private String nickname;

}

and i'm using postman.

POST /test? HTTP/1.1
Host: localhost
Content-Type: application/json
cache-control: no-cache
[
    {
        "email": "[email protected]",
        "nickname": "test1"
    },
    {
        "email": "[email protected]",
        "nickname": "test2"
    }
]

and output:

2019-05-24 11:04:40.871  INFO 1860 --- [p-nio-80-exec-5] k.c.k.w.w.s.controller.HomeController    : 2

also i debugged to check my userDTO. the email and nickname was set to the parameters i sent in postman.

changing parameter like this:

[
  {
    "idtipounidad": 1,
    "modelo": 2013,
    "cantidad": 1,
    "tipopago": 1,
    "costounidad": 1500000,
    "ivaoperacion": 25000,
    "totaloperacion": 1504000,
    "enganche": 750000,
    "idplazo": 3
  },
  {
    "idtipounidad": 2,
    "modelo": 2012,
    "cantidad": 2,
    "tipopago": 2,
    "costounidad": 1500000,
    "ivaoperacion": 25000,
    "totaloperacion": 1504000,
    "enganche": 750000,
    "idplazo": 6
  }
]

and your controller:

@ResponseBody
@RequestMapping(value = "/ajax/abrir_cotizacion", method = RequestMethod.POST)
public Object abrircotizacion(Model model, HttpServletRequest request, @RequestBody Unidad[] unidad) {
   ....
}

i hope this work.. And try postman. This is quite useful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.