When making a DELETE request, the org.springframework.web.Httprequestmethodnotsupportedexception error is returned

Asked

Viewed 220 times

1

When making a request to delete a user the following error is returned:

{
    "timestamp": 1500142937327,
    "status": 405,
    "error": "Method Not Allowed",
    "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
    "message": "Request method 'DELETE' not supported",
    "path": "/api/2"
}

follows the methods being used,:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/api")
public class ControllerUsuario {

    @Autowired
    UsuarioService clienteservice;

    @Autowired
    SerieService serieService;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Usuario> buscaUsuario(@PathVariable("id") Long id) {

        Usuario usu = clienteservice.buscarUsuario(id);
        if (usu != null) {
            System.out.println(usu.getSeries().toString());
            return new ResponseEntity<>(usu, HttpStatus.ACCEPTED);
        }
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public ResponseEntity<Usuario> deletarCliente(@PathVariable Long id) {

        if (clienteservice.remover(id)) {
            return new ResponseEntity<>(HttpStatus.OK);
        }

        return new ResponseEntity<>(HttpStatus.NOT_FOUND);

    }

    @RequestMapping( method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Usuario> cadastrar(@RequestBody Usuario usuario) {
        System.out.println(usuario.getId());
        return new ResponseEntity<>(clienteservice.cadastro(usuario), HttpStatus.OK);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Serie> adicionarSerie(@RequestBody Serie serie, @PathVariable("id") Long id) {
        Usuario usu = clienteservice.buscarUsuario(id);
        serieService.adicionarSerie(serie);
        usu.adicionarSerie(serieService.buscarSerie(serie.getId()));
        clienteservice.cadastro(usu);
        System.out.println(usu.getSeries().toString());
        return new ResponseEntity<Serie>(serie, HttpStatus.OK);
    }
}

public boolean remover(Long id) {

    if(usuariorepositorio.exists(id)) {
        usuariorepositorio.delete(id);  
        return true;
    }
    return false;
}

POST and GET are functioning normally.

No answers

Browser other questions tagged

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