Returning data from Java to Ajax using Json

1

I have a Java application, and a module that inserts products into the base using REST. In the base the id of the product is autoincremental, and as soon as I add the same in the base, I need the id so that the table with the products are updated with the id correctly. Here are the codes:

AJAX:

$.ajax({
        type: 'POST',
        url: '/minhaaplicacao/rest/produto',
        data: dataJson,
        contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                **//aqui preciso pegar o ID, pelo objeto 
                    //que foi adicionado na base de dados
                    //e retornar para o novoId.
                    //o commit true atualiza a tabela no html, 
                    //inserido o novo registro, o novoId 
                    //é o valor que preciso pegar, 
                    //o qual foi inserido na base** 
                commit(true, novoId);
        }, 
        error: function () {
            commit(false);
            alert('deu erro');
        }
    });

On the server side I have java returning a response with the product object, with all the information entered in the database:

JAVA:

@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response adicionar(Produto produto)
{
    Produto produtoInserido = ProdutoService.insert(produto);
    return Response.ok().entity(produtoInserido).build();
}

It happens that the id of the product is auto incremental, and I have the value just after insertion, and I need it to return to the ajax, so that the product listing table is updated with correct id.

    
asked by anonymous 27.06.2014 / 15:26

1 answer

4

Just in the return, put a parameter, as I show in the ex below:

$.ajax({
        type: 'POST',
        url: '/minhaaplicacao/rest/produto',
        data: dataJson,
        contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                    TEUID = r.Id
        }, 
        error: function () {
            commit(false);
            alert('deu erro');
        }
    });
    
27.06.2014 / 15:38