Use . POST or . DELETE method for request to delete data

Asked

Viewed 951 times

0

I was using . delete but suddenly the method is not being recognized from the error in the console.

2017-04-12 09:51:34.022 WARN 5836 --- [nio-8080-exec-9] o.s.web.servlet.Pagenotfound: Request method 'POST' not supported

I’m sending it like this in html.

  <form method="post">
        <input type="hidden" name="_method" value="DELETE"/> </form>

and my controller is like this.

@PreAuthorize("hasAuthority('PERM_MANTENEDORA_EXCLUIR')")
@RequestMapping(value = "/excluir/{id}", method = RequestMethod.DELETE)*****************
public String excluirMantenedora(@PathVariable("id") Mantenedora mantenedora, RedirectAttributes attributes) {

    if (mantenedora.getSituacao().getNome() == "ativo") {
        try {
            // condição para não deixar EXCLUIR quando tiver algum vínculo.
            List<Instituicao> instituicoes = instituicaoRepository.findByMantenedoraId(mantenedora.getId());
            if (instituicoes.size() <= 0) {
                mantenedoraService.excluir(mantenedora.getId());
                mensagem = mensagemSucesso(MensagemEnum.MENSAGEM_SUCESSO_EXCLUIR.getMensagem());
            } else {
                mensagem = mensagemErro(MensagemEnum.MENSAGEM_ERRO_VINCULO.getMensagem());
            }
        } catch (CustomException e) {
            mensagem = mensagemErro(e.getMessage());
        }

        attributes.addFlashAttribute("mensagem", mensagem);
    }
    return "redirect:/mantenedora";
}

If I change DELETE to POST works but I want to know why it happened that the DELETE method is not recognized.

  • you need to change in your form the method of <form method="post"> for <form method="delete">. This occurs because your method expects a call via Delete, RequestMethod.DELETE but within your form you are passing the call via post <form method="post">

  • vlw for the tip but I tried and now gave error in Get.

  • 2017-04-12 10:34:40.317 WARN 3788 --- [nio-8080-exec-6] o.s.web.servlet.Pagenotfound: Request method 'GET' not supported

  • GET, POST, PUT and DELETE (there are others) are a part of the pattern HTTP, but from what I’ve seen, forms HTML are limited to GET and POST. A solution would actually use as POST or still use PUT and DELETE with requests AJAX. To this end, consutle http://api.jquery.com/jQuery.ajax/

No answers

Browser other questions tagged

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