Webservice delete error [closed]

0

I have a problem with my webservice. You are 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;
}   
    
asked by anonymous 27.09.2017 / 15:52

1 answer

1

The 405 error is Method not allowed . This error indicates that there was a request at a given URL using an HTTP verb that was not appropriate.

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 while it's not what you asked yourself, you can 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 boolean s to indicate errors, this is bad programming practice. The exceptions were designed exactly so that programmers did not have to do this, so take advantage of that feature that language offers you.

And never leave% s of% s out there hanging around. The best way to manage them is through try-with-resources .

    
27.09.2017 / 16:05