Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Method not allowed "Request method 'PUT' not supported"

Olá, estou recebendo o seguinte erro:

{timestamp: 1512065616839, status: 405, error: "Method Not Allowed",…}
error:"Method Not Allowed"
exception:"org.springframework.web.HttpRequestMethodNotSupportedException"
message:"Request method 'PUT' not supported"
path:"/api/v1/products/1"
status:405
timestamp:1512065616839

com o seguinte header

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:4200
Allow:DELETE, GET
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Type:application/json;charset=UTF-8
Date:Thu, 30 Nov 2017 18:13:36 GMT
Expires:0
Pragma:no-cache
Transfer-Encoding:chunked
Vary:Origin
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block

claramente o método PUT não está habilitado. Gostaria de saber como habilitá-lo.

Esse é o meu controller


import java.util.List;

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.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import br.com.francisco.pemaza.crud.models.Products;
import br.com.francisco.pemaza.crud.services.ProductsService;

@RestController
@RequestMapping("/api/v1/products")
@CrossOrigin("${origin-allowed}")
public class ProductsController {

    @Autowired
    private ProductsService productsService;

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<Products>> all() {

        List<Products> products = this.productsService.getAll();

        return new ResponseEntity<List<Products>>(products, HttpStatus.OK);
    }

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Products> register(@RequestBody Products product) {

        Products productSaved = productsService.register(product);

        return new ResponseEntity<Products>(productSaved, HttpStatus.CREATED);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Products> removeProduct(@PathVariable("id") Integer id) {

        Products productFound = productsService.findById(id);
        if (productFound == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        productsService.remove(productFound);

        return new ResponseEntity<Products>(HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Products> findById(@PathVariable("id") Integer id) {

        Products productFound = productsService.findById(id);
        if (productFound == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<Products>(productFound, HttpStatus.OK);
    }

    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Products> modify(@RequestBody Products product) {

        Products productChanged = productsService.modify(product);

        return new ResponseEntity<Products>(productChanged, HttpStatus.OK);
    }
}

Obrigado!

2 respostas

Oi Francisco, esse endereço path:"/api/v1/products/1" realmente não está liberado para put, ta mapeado para get.

solução!

Agora que percebi que tanto o get quanto o put estão usando a mesma uri. Talvez se eu mudar um deles resolva.