GET instead of DELETE in the REST webservice

Asked

Viewed 155 times

0

I’m trying to delete my bank using a webservice rest(JAVA), he carries everything neat, but time I send the test he Method not allowed.

Note: I am using Netbeans.

inserir a descrição da imagem aqui

From what I understand the WS is trying to accomplish a GET and not a DELETE: GET SolicitaçãoFailed RequestFailed --> Status: (405)

then I looked at my class and I have no idea what’s wrong:

WS:

@DELETE
    @Path("Banco/delete/{id}")
    public String deleteBanco(@PathParam("id") int id){

        BancoDAO dao = new BancoDAO();
        String resposta = dao.deleteBanco(id);

        Gson gson = new Gson();
        return gson.toJson(resposta);
    }

DAO:

public String deleteBanco(int codigo) {
        try {
            conn = Conexao.obtemConexao();
            String delete = "DELETE FROM BANCO WHERE BCO_COIGO = '?'";

            stmt = conn.prepareStatement(delete);
            int i = stmt.executeUpdate();

            if (i == 0) {
                return "Erro ao excluir banco";
            } else if (i == 1) {
                return "Banco removido com sucesso...";
            } else if (i > 1) {
                return "Erro 001: \n Problema de SQL, mais de uma informação "
                        + "foi removida, favor entrar em contado com suporte "
            } else {
                return "Erro 301: \n Erro não identificado, "
                        + "favor entrar em contato com suporte.";
            }

        } catch (Exception e) {
            return "Erro ao excluir banco: " + e.getMessage();
        }
    }

Only GET method I own:

@GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("Banco/get/{nome}")
    public String getBanco(@PathParam("nome") String nome){
        List<BancoCTR> lista = new ArrayList<BancoCTR>();
        BancoDAO banco = new BancoDAO();

        if (nome.equals("null")){
            lista = banco.listBanco(0, nome);
        }else{
            lista = banco.listBanco(1, nome);
        }

        Gson gson = new Gson();
        return gson.toJson(lista);
    }
  • 1

    If you take a hint out of your question: looking at the Rest addresses you created, I suggest you take a look at changing them to become more adherent to good practices. Example: change from Banco/delete/{id}" to just bancos/{id}"

  • If you are testing in the browser, remember that it only makes requests via GET, to make a request via DELETE you can use a tool like Postman, as already suggested by LR10 in its reply, and taking advantage of @Dherik’s comment, it is worth you look for some theoretical material on the subject, because really your mapping was not very consistent with the Rest architecture, it is important to understand the proposal, and so you will know how to use it in the best way.

1 answer

0

Your client is making a request of the kind GET changes to the request to DELETE as in the example below. I am using POSTMAN

your eve URL gets more or less to yes: localhost:8080/Banco/delete/1

inserir a descrição da imagem aqui

Browser other questions tagged

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