如何使用Rest、Jax rs、jersey和Javascript进行一对一关系的升级(PUT)?

如何使用Rest、Jax rs、jersey和Javascript进行一对一关系的升级(PUT)?,javascript,java,rest,jersey,jax-rs,Javascript,Java,Rest,Jersey,Jax Rs,所以我尝试了一段时间,但什么都没有,我用一个教程用一个类(实体)做了一个简单的CRUD,一切都很好,但随后我开始稍微增加实体的数量,并在本例中使用一种关系,即ServiceOrdem具有客户端和服务,客户端与用户和访问级别有关系,所以在这个例子中,使用Java我认为它非常简单,我做了一个测试类,在服务器端一切都很好,但是我在客户端遇到了很多麻烦,所以我做错了什么。在这种情况下,我可以在客户端正确地进行选择并查看信息,甚至可以删除信息,但我不能插入新信息或更新具有关系的字段。在这种情况下,我的Se

所以我尝试了一段时间,但什么都没有,我用一个教程用一个类(实体)做了一个简单的CRUD,一切都很好,但随后我开始稍微增加实体的数量,并在本例中使用一种关系,即ServiceOrdem具有客户端服务客户端用户访问级别有关系,所以在这个例子中,使用Java我认为它非常简单,我做了一个测试类,在服务器端一切都很好,但是我在客户端遇到了很多麻烦,所以我做错了什么。在这种情况下,我可以在客户端正确地进行选择并查看信息,甚至可以删除信息,但我不能插入新信息或更新具有关系的字段。在这种情况下,我的ServiceOrder实体

我的测试主类的Java代码(Java):

//Updating OrdemDeServico entity.

      //here I select a service(Servico) for his ID then I put the information in an object(serv)
        ServicoDAO sdao = new ServicoDAO();
        Servico serv = sdao.porId(2);
          //here I select a user(User) for his ID then I put the information in an object(usu)
        UsuarioDAO udao = new UsuarioDAO();
        Usuario usu = udao.porId(4);
          //here I select a accessLevel(Nivel) for his ID then I put the information in an object(niv)
        NivelDAO niDAO = new NivelDAO();
        Nivel niv = niDAO.porId(2);
//Then I select the client(Cliente) for his ID put the information in an object(cli) and in the fields with relationship I put the objects that I created before.
        ClienteDAO cliDAO = new ClienteDAO();
        Cliente cli = cliDAO.porId(3);
        cli.setNivel(niv);
        cli.setUsuario(usu);
//Here is where I update the ServiceOrder(OrdemDeServico)entity what I want is to change just the field status the other ones I leave as they are.

        OrdemDeServicoDAO dao = new OrdemDeServicoDAO();
        OrdemDeServico ordem = new OrdemDeServico(2);
        ordem.setStatus(2);
        ordem.setCodServico(serv);
        ordem.setCodCliente(cli);

        dao.atualizar(ordem);
        System.out.println(" " + ordem.getStatus());
@Path("/ordens")

public class OrdemDeServicoResource {
OrdemDeServicoDAO dao = new OrdemDeServicoDAO();

@PUT
@Path("{id}")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public OrdemDeServico atualizar(OrdemDeServico ordem)throws Exception{
    System.out.println("Atualização de ordem de servico:" +ordem.getStatus());
    dao.atualizar(ordem);
    return ordem;
}

@DELETE @Path("{id}")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public void remover(@PathParam("id")int id)throws Exception{
    OrdemDeServico ordem = dao.porId(id);
    dao.delete(ordem);

}
var rootURL ="http://localhost:8080/TCC02/rest";

listOrder();

function listOrder(){
    var id = getId();
    $("#orId").attr("data-value",id);   
    if(id<1){
        alert('id invalido');
        return;
    }
    console.log('getOrder ' +id);
    $.getJSON(rootURL + '/ordens/' + id, function(data){
        if(!data){
            alert('Ordem de servico não encontrada');
            return
        }
    var ordem = data;
    renderDetails(ordem);
    return var oordem = data;   

    });
}

function porIdSer(id3){
    console.log('Find the service for the ID: ' +id3);
    $.getJSON(rootURL + '/servicos/' +  id3, function(data){
        if(!data){
            alert('Service wasn't found');
            return
        }
        var ser = data;
        renderDetailsSer(ser);
        return var sser = data;
    });
}

function porIdLogin(id4){
    console.log('Find the User for the ID: ' +id4);
    $.getJSON(rootURL +'/usuarios/' + id4, function(data){
        if(!data){
            alert('User wasn't found');
            return
        }
        var usu = data;
        renderDetailsUsu(usu);
        return var uusu = data;
    });
}

function porIdLevel(id5){
    console.log('Find the level for the ID: ' + id5);
    $.getJSON(rootURL+ '/niveis/'+ id5,function(data){
        if(!data){
            alert('ID wasn't found');
            return
        }
        var niv = data;
        renderDetailsNiv(niv);  
        return var nniv = data;
    });
}

function porIdCli(id2){
    console.log('Find the cliente for the ID:' + id2);
    $.getJSON(rootURL + '/clientes/' + id2, function(data){
        if(!data){
            alert('Client wasn't found');
            return
        }
        var cli = data;
        renderDetailsCli(cli);
        return var clli = data;
    });
}
    $('#btnAtt').click(function(){
        $("#myModal").modal();
        console.log('Loading details');
        var id = getId();
        if(id<1){
            alert ('Invalid ID');
            return;
        }
        console.log ('getOrdem' + id);
        $.getJSON(rootURL+'/ordens/' + id, function(data){
            if(!data){
                alert('Id wasn't found');
                return
            }
            var ordem = data;
            renderDetails1(ordem);
        });
    });

    $('#btnSave').click(function(){
        updateOrder();
    });

    function updateOrder(){
        var ident = $("#orId").attr("data-value");
        console.log('Update service order' +ident);
        $.ajax({
            type:'PUT',
            contentType: 'application/json',
            url:rootURL + '/ordens/' + ident,
            dataType:"json",
            data:formToJSON(),
            success:function(data,textStatus,jqXHR){
                alert('Updated with success');  
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert('Something wrong in the updateOrder ' + errorThrown);

            }
        });
    }
    function DeleteOrder(){
        console.log('Delete Service Order');
        $.ajax({
            type:'DELETE',
            url:rootURL + '/' + $('#orId').val(),
            success: function(data, textStatus,jqXHR){
            alert('Order deleted with success');    
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert('Error in the delete')
            }       
        });
    }
function getId(){
    //Search and catch the id in the URL
    var id = window.location.hash.substr(1);
    //Confirm if the ID it's a INT
    var intRegex = /^\d+$/;
    return intRegex.test(id)? id : -1;
}
function renderDetails(ordem){
    $('#orId').val(ordem.id);
    $('#nomecliente').val(ordem.codCliente.nome);
    $('#servico').val(ordem.codServico.nomeservico);
    $('#codserv').val(ordem.codServico.id);
    $('#codcli').val(ordem.codCliente.id);
    $('#status').append('<option>' + ordem.status + '</option>');

    //Defining the data-value with the values from codCliente and codServico.
    var atributo2 = ordem.codServico.id;
    $("#codserv").attr("data-value",atributo2);
    var atributo = ordem.codCliente.nivel.id
    $("#codcli").attr("data-value",atributo);

    porIdCli(atributo);
    porIdSer(atributo2);
}

function formToJSON(){
    var orId = $('#orId1').val();
    return JSON.stringify({
        "id": orId == "" ? null : orId,
        "status": $('#status1').val(),
        "servico": $('#servico1').data(),
        "nomecliente":$('#nomecliente1').data(),    
    });
我的一个资源的代码(我正在展示我认为必要的内容)(JAVA):

//Updating OrdemDeServico entity.

      //here I select a service(Servico) for his ID then I put the information in an object(serv)
        ServicoDAO sdao = new ServicoDAO();
        Servico serv = sdao.porId(2);
          //here I select a user(User) for his ID then I put the information in an object(usu)
        UsuarioDAO udao = new UsuarioDAO();
        Usuario usu = udao.porId(4);
          //here I select a accessLevel(Nivel) for his ID then I put the information in an object(niv)
        NivelDAO niDAO = new NivelDAO();
        Nivel niv = niDAO.porId(2);
//Then I select the client(Cliente) for his ID put the information in an object(cli) and in the fields with relationship I put the objects that I created before.
        ClienteDAO cliDAO = new ClienteDAO();
        Cliente cli = cliDAO.porId(3);
        cli.setNivel(niv);
        cli.setUsuario(usu);
//Here is where I update the ServiceOrder(OrdemDeServico)entity what I want is to change just the field status the other ones I leave as they are.

        OrdemDeServicoDAO dao = new OrdemDeServicoDAO();
        OrdemDeServico ordem = new OrdemDeServico(2);
        ordem.setStatus(2);
        ordem.setCodServico(serv);
        ordem.setCodCliente(cli);

        dao.atualizar(ordem);
        System.out.println(" " + ordem.getStatus());
@Path("/ordens")

public class OrdemDeServicoResource {
OrdemDeServicoDAO dao = new OrdemDeServicoDAO();

@PUT
@Path("{id}")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public OrdemDeServico atualizar(OrdemDeServico ordem)throws Exception{
    System.out.println("Atualização de ordem de servico:" +ordem.getStatus());
    dao.atualizar(ordem);
    return ordem;
}

@DELETE @Path("{id}")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public void remover(@PathParam("id")int id)throws Exception{
    OrdemDeServico ordem = dao.porId(id);
    dao.delete(ordem);

}
var rootURL ="http://localhost:8080/TCC02/rest";

listOrder();

function listOrder(){
    var id = getId();
    $("#orId").attr("data-value",id);   
    if(id<1){
        alert('id invalido');
        return;
    }
    console.log('getOrder ' +id);
    $.getJSON(rootURL + '/ordens/' + id, function(data){
        if(!data){
            alert('Ordem de servico não encontrada');
            return
        }
    var ordem = data;
    renderDetails(ordem);
    return var oordem = data;   

    });
}

function porIdSer(id3){
    console.log('Find the service for the ID: ' +id3);
    $.getJSON(rootURL + '/servicos/' +  id3, function(data){
        if(!data){
            alert('Service wasn't found');
            return
        }
        var ser = data;
        renderDetailsSer(ser);
        return var sser = data;
    });
}

function porIdLogin(id4){
    console.log('Find the User for the ID: ' +id4);
    $.getJSON(rootURL +'/usuarios/' + id4, function(data){
        if(!data){
            alert('User wasn't found');
            return
        }
        var usu = data;
        renderDetailsUsu(usu);
        return var uusu = data;
    });
}

function porIdLevel(id5){
    console.log('Find the level for the ID: ' + id5);
    $.getJSON(rootURL+ '/niveis/'+ id5,function(data){
        if(!data){
            alert('ID wasn't found');
            return
        }
        var niv = data;
        renderDetailsNiv(niv);  
        return var nniv = data;
    });
}

function porIdCli(id2){
    console.log('Find the cliente for the ID:' + id2);
    $.getJSON(rootURL + '/clientes/' + id2, function(data){
        if(!data){
            alert('Client wasn't found');
            return
        }
        var cli = data;
        renderDetailsCli(cli);
        return var clli = data;
    });
}
    $('#btnAtt').click(function(){
        $("#myModal").modal();
        console.log('Loading details');
        var id = getId();
        if(id<1){
            alert ('Invalid ID');
            return;
        }
        console.log ('getOrdem' + id);
        $.getJSON(rootURL+'/ordens/' + id, function(data){
            if(!data){
                alert('Id wasn't found');
                return
            }
            var ordem = data;
            renderDetails1(ordem);
        });
    });

    $('#btnSave').click(function(){
        updateOrder();
    });

    function updateOrder(){
        var ident = $("#orId").attr("data-value");
        console.log('Update service order' +ident);
        $.ajax({
            type:'PUT',
            contentType: 'application/json',
            url:rootURL + '/ordens/' + ident,
            dataType:"json",
            data:formToJSON(),
            success:function(data,textStatus,jqXHR){
                alert('Updated with success');  
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert('Something wrong in the updateOrder ' + errorThrown);

            }
        });
    }
    function DeleteOrder(){
        console.log('Delete Service Order');
        $.ajax({
            type:'DELETE',
            url:rootURL + '/' + $('#orId').val(),
            success: function(data, textStatus,jqXHR){
            alert('Order deleted with success');    
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert('Error in the delete')
            }       
        });
    }
function getId(){
    //Search and catch the id in the URL
    var id = window.location.hash.substr(1);
    //Confirm if the ID it's a INT
    var intRegex = /^\d+$/;
    return intRegex.test(id)? id : -1;
}
function renderDetails(ordem){
    $('#orId').val(ordem.id);
    $('#nomecliente').val(ordem.codCliente.nome);
    $('#servico').val(ordem.codServico.nomeservico);
    $('#codserv').val(ordem.codServico.id);
    $('#codcli').val(ordem.codCliente.id);
    $('#status').append('<option>' + ordem.status + '</option>');

    //Defining the data-value with the values from codCliente and codServico.
    var atributo2 = ordem.codServico.id;
    $("#codserv").attr("data-value",atributo2);
    var atributo = ordem.codCliente.nivel.id
    $("#codcli").attr("data-value",atributo);

    porIdCli(atributo);
    porIdSer(atributo2);
}

function formToJSON(){
    var orId = $('#orId1').val();
    return JSON.stringify({
        "id": orId == "" ? null : orId,
        "status": $('#status1').val(),
        "servico": $('#servico1').data(),
        "nomecliente":$('#nomecliente1').data(),    
    });
我的服务的Javascript代码或详细信息(我认为问题就在这里):

//Updating OrdemDeServico entity.

      //here I select a service(Servico) for his ID then I put the information in an object(serv)
        ServicoDAO sdao = new ServicoDAO();
        Servico serv = sdao.porId(2);
          //here I select a user(User) for his ID then I put the information in an object(usu)
        UsuarioDAO udao = new UsuarioDAO();
        Usuario usu = udao.porId(4);
          //here I select a accessLevel(Nivel) for his ID then I put the information in an object(niv)
        NivelDAO niDAO = new NivelDAO();
        Nivel niv = niDAO.porId(2);
//Then I select the client(Cliente) for his ID put the information in an object(cli) and in the fields with relationship I put the objects that I created before.
        ClienteDAO cliDAO = new ClienteDAO();
        Cliente cli = cliDAO.porId(3);
        cli.setNivel(niv);
        cli.setUsuario(usu);
//Here is where I update the ServiceOrder(OrdemDeServico)entity what I want is to change just the field status the other ones I leave as they are.

        OrdemDeServicoDAO dao = new OrdemDeServicoDAO();
        OrdemDeServico ordem = new OrdemDeServico(2);
        ordem.setStatus(2);
        ordem.setCodServico(serv);
        ordem.setCodCliente(cli);

        dao.atualizar(ordem);
        System.out.println(" " + ordem.getStatus());
@Path("/ordens")

public class OrdemDeServicoResource {
OrdemDeServicoDAO dao = new OrdemDeServicoDAO();

@PUT
@Path("{id}")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public OrdemDeServico atualizar(OrdemDeServico ordem)throws Exception{
    System.out.println("Atualização de ordem de servico:" +ordem.getStatus());
    dao.atualizar(ordem);
    return ordem;
}

@DELETE @Path("{id}")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public void remover(@PathParam("id")int id)throws Exception{
    OrdemDeServico ordem = dao.porId(id);
    dao.delete(ordem);

}
var rootURL ="http://localhost:8080/TCC02/rest";

listOrder();

function listOrder(){
    var id = getId();
    $("#orId").attr("data-value",id);   
    if(id<1){
        alert('id invalido');
        return;
    }
    console.log('getOrder ' +id);
    $.getJSON(rootURL + '/ordens/' + id, function(data){
        if(!data){
            alert('Ordem de servico não encontrada');
            return
        }
    var ordem = data;
    renderDetails(ordem);
    return var oordem = data;   

    });
}

function porIdSer(id3){
    console.log('Find the service for the ID: ' +id3);
    $.getJSON(rootURL + '/servicos/' +  id3, function(data){
        if(!data){
            alert('Service wasn't found');
            return
        }
        var ser = data;
        renderDetailsSer(ser);
        return var sser = data;
    });
}

function porIdLogin(id4){
    console.log('Find the User for the ID: ' +id4);
    $.getJSON(rootURL +'/usuarios/' + id4, function(data){
        if(!data){
            alert('User wasn't found');
            return
        }
        var usu = data;
        renderDetailsUsu(usu);
        return var uusu = data;
    });
}

function porIdLevel(id5){
    console.log('Find the level for the ID: ' + id5);
    $.getJSON(rootURL+ '/niveis/'+ id5,function(data){
        if(!data){
            alert('ID wasn't found');
            return
        }
        var niv = data;
        renderDetailsNiv(niv);  
        return var nniv = data;
    });
}

function porIdCli(id2){
    console.log('Find the cliente for the ID:' + id2);
    $.getJSON(rootURL + '/clientes/' + id2, function(data){
        if(!data){
            alert('Client wasn't found');
            return
        }
        var cli = data;
        renderDetailsCli(cli);
        return var clli = data;
    });
}
    $('#btnAtt').click(function(){
        $("#myModal").modal();
        console.log('Loading details');
        var id = getId();
        if(id<1){
            alert ('Invalid ID');
            return;
        }
        console.log ('getOrdem' + id);
        $.getJSON(rootURL+'/ordens/' + id, function(data){
            if(!data){
                alert('Id wasn't found');
                return
            }
            var ordem = data;
            renderDetails1(ordem);
        });
    });

    $('#btnSave').click(function(){
        updateOrder();
    });

    function updateOrder(){
        var ident = $("#orId").attr("data-value");
        console.log('Update service order' +ident);
        $.ajax({
            type:'PUT',
            contentType: 'application/json',
            url:rootURL + '/ordens/' + ident,
            dataType:"json",
            data:formToJSON(),
            success:function(data,textStatus,jqXHR){
                alert('Updated with success');  
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert('Something wrong in the updateOrder ' + errorThrown);

            }
        });
    }
    function DeleteOrder(){
        console.log('Delete Service Order');
        $.ajax({
            type:'DELETE',
            url:rootURL + '/' + $('#orId').val(),
            success: function(data, textStatus,jqXHR){
            alert('Order deleted with success');    
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert('Error in the delete')
            }       
        });
    }
function getId(){
    //Search and catch the id in the URL
    var id = window.location.hash.substr(1);
    //Confirm if the ID it's a INT
    var intRegex = /^\d+$/;
    return intRegex.test(id)? id : -1;
}
function renderDetails(ordem){
    $('#orId').val(ordem.id);
    $('#nomecliente').val(ordem.codCliente.nome);
    $('#servico').val(ordem.codServico.nomeservico);
    $('#codserv').val(ordem.codServico.id);
    $('#codcli').val(ordem.codCliente.id);
    $('#status').append('<option>' + ordem.status + '</option>');

    //Defining the data-value with the values from codCliente and codServico.
    var atributo2 = ordem.codServico.id;
    $("#codserv").attr("data-value",atributo2);
    var atributo = ordem.codCliente.nivel.id
    $("#codcli").attr("data-value",atributo);

    porIdCli(atributo);
    porIdSer(atributo2);
}

function formToJSON(){
    var orId = $('#orId1').val();
    return JSON.stringify({
        "id": orId == "" ? null : orId,
        "status": $('#status1').val(),
        "servico": $('#servico1').data(),
        "nomecliente":$('#nomecliente1').data(),    
    });
var rootURL=”http://localhost:8080/TCC02/rest";
listOrder();
函数listOrder(){
var id=getId();
$(“#orId”).attr(“数据值”,id);

如果(请发送一个关于代码片段和错误跟踪的特定问题。我们无法分析您的项目。哪个部分不起作用javascript?java代码?插入?bbdd?好的,所以我的java代码工作得很好,问题出在客户端,所以在我的javascript中,特别是在我的函数更新中,它不起作用有人吗?我尝试了一些方法,但什么都没有,这可能是某种资源问题,我应该添加一些子资源?或者这只是JavaScript中的UpdateOrder函数的问题?