如何使用jquery和springboot同时发布多个参数?

kq4fsx7k  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(141)

我想知道是否有一种方法可以将数据作为单独的请求参数来接收,而不是通过创建一个新类来解决问题,该类包含我想接收的其他类。
这是我的控制器上的Map请求:

@PostMapping(value ="/save", consumes={"application/json;charset=utf-8"})
    public ResponseEntity<Cliente> save(@RequestBody ClienteSaveBody json) {
        Cliente persona = json.getCliente();
        Ubicacion location = json.getUbicacion();
        String nSector = json.getSector();

        Sector sector = sectorRepo.findById(nSector).orElseThrow(() -> new EntityNotFoundException("sector no encontrado para este id:: " + nSector));
        location.setSector(sector);
        location.setCliente(persona);
        persona.setUbicacion(location);
        Cliente obj = clienteRepo.save(persona);
        return new ResponseEntity<Cliente>(obj, HttpStatus.OK);
    }

public class ClienteSaveBody {
    Cliente cliente;
    Ubicacion ubicacion;
    String sector;
}

这是我从jquery发送数据的方式:

var ubicacion={
        calle: $('#calle').val(),
        casa: $('#casa').val(),
        };

        var data={
            cliente:cliente,
            ubicacion:ubicacion,
            nombreSector:$('#sector').val()
        };
        app.save(
            data
        );

save : function(data){ //api call save
    $.ajax({
        url: app.backend + '/clientes/save',///save/' + sector,
        data : JSON.stringify({cliente: data.cliente, ubicacion: data.ubicacion, sector: data.nombreSector}),
        method: 'POST',
        dataType : 'json',
        contentType: 'application/json; charset=utf-8',
        success : function(json){
            $('#msg').text('Se guardo correctamente');
            $('#msg').show();
            $('#clienteModal').modal('hide');
            app.table.ajax.reload();
        },
        error : function (error){
            $('#msg').text(error.error);
            $('#msg').show();
        }
    })
},

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题