Webservice delete error

Asked

Viewed 101 times

0

I have a problem in my webservice. It is giving this error:

Requestfailed Requestfailed --> Status: (405)

The code I’m using:

@DELETE
@Produces("text/plain")
@Path("ExcluirLista/{usuario}")
public boolean excluirTodos(@PathParam("usuario") String usuario)
{
    ProdutoDAO dao = new ProdutoDAO();
    return dao.excluir(usuario);
}

public boolean excluir(String usuario)
{
    String sql = "delete * from listaproduto where uclogin=?";
    Boolean retorno = false;
    PreparedStatement pst = Conexao.getPreparedStatement(sql);
    try {
        pst.setString(1,usuario);
        if(pst.executeUpdate()>0)
        {
            retorno = true;
        }   
    } catch (SQLException ex) {
        Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
        retorno = false;
    }
    return retorno;
}   

1 answer

1

Error 405 is the Method not allowed. This error indicates that there was a request in a given URL using an HTTP verb that was not suitable.

The @DELETE makes it clear that the request should be made using the HTTP DELETE method. If this error was produced, it is because some other HTTP method (such as POST) was used instead.


And although it’s not what you asked, it’s possible to improve your code like this:

@DELETE
@Produces("text/plain")
@Path("ExcluirLista/{usuario}")
public void excluirTodos(@PathParam("usuario") String usuario) {
    ProdutoDAO dao = new ProdutoDAO();
    dao.excluir(usuario);
}

public void excluir(String usuario) {
    String sql = "delete * from listaproduto where uclogin = ?";

    try (PreparedStatement pst = Conexao.getPreparedStatement(sql)) {
        pst.setString(1, usuario);
        if (pst.executeUpdate() == 0) throw new RuntimeException();
    } catch (SQLException ex) {
        Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}

Do not use booleans to indicate errors, this is bad programming practice. The exceptions were designed exactly so that programmers didn’t have to do this, and so take advantage of this feature that language offers you.

And never let PreparedStatementopen up for nothing. The best way to manage them is through Try-with-Resources.

  • Even using the codes as you passed, it gave error 405, what I realized is that on top of my search in the test, it does not appear the url that will be used, but in the other get’s and post’s that I have shows...

  • @Renatocrispim These codes will not change the HTTP status. The cause of this error is in the code that tries to call your method excluirTodos. This method is not even being invoked because it must be being called with the incorrect HTTP method.

  • But why does this happen? Would you tell me? Because if I create a post or get it calls normally.

  • @Renatocrispim Because the code that is trying to invoke your method is not using DELETE. Maybe it is using POST or GET.

  • I can’t fix it. What can I do?

  • 1

    How are you doing to call the url?

  • @Renatocrispim Post the code you use to call your service. Without it you can not tell you how to fix.

  • Do you say code on another system that calls the webservice? I’m just testing it in netbeans... All about what’s giving problem is there... I’m just creating the delete and ta giving error, as I said if I create a post it works...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.