0
votes

I have Java controller:

@RequestMapping("tep")
public class TepController {

    private final TepRepo repo;

    @Autowired
    public TepController(TepRepo repo) {
        this.repo = repo;
    }

    @GetMapping
    public List<Tep> list(){
        return  repo.findAll();
    }
    @PostMapping
    public Tep create(@RequestBody Tep tep){
        return  repo.save(tep);
    }
    @GetMapping("{id}")
    public Tep getOne(@PathVariable("id") Tep tep){
        return tep;
    }

    @PutMapping("{id}")
    public Tep sent(@PathVariable("id") Tep tepFromDb,
                    @RequestBody Tep tep){
        BeanUtils.copyProperties(tep, tepFromDb, "id");
        return repo.save(tepFromDb);
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Tep tep){
        repo.delete(tep);
    }
}

And ran into the following problem: WARN 14068 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported] It happened when I send DELETE request, but others requests work good

Thank for answers)

3
@ResponseBody is missingShubham
I don't think POJO (Tep) can be used as path variable. PathVariable should be a simple type - number, String, Date, etc. Try using a number type as PathVariable.Beri
can you share how you call the DELETE API? as well as the other REST API where it worked? I'm assuming the error is related to how you are calling it.yellowvamp04
deleteItem (item) { const index = this.teps.indexOf(item) this.$resource('/tep{/id}').remove(index).then(result => {if (result.ok){this.teps.splice(index, 1)}}) /*confirm('Вы действительно хотите удалить значение?') && */ }Vitaly
The Problem was solved: deleteItem (item) { const index = item.id const index2 = this.teps.indexOf(item) confirm('Вы действительно хотите удалить значение?') && this.$resource('/tep/'+index).remove(index).then(result => {if (result.ok){this.teps.splice(index, 1)}}) this.teps.splice(index2, 1)}, THENK all for answers :)Vitaly

3 Answers

0
votes

I'd agree with Beri's comment that this should really be a primitive type, ideally an int/string as I'm not sure of how spring boot handles DELETE having a body. There's nothing to say you can't have a body in a DELETE but some implementations require that there is no body.

0
votes
@DeleteMapping("/{id}")

Your mapping contains "/" which is not present in other http methods. Please share the Http request url that you are using to test.

0
votes

Add this line to you application.properties file if you have one, else create this file unders resources and write this line

spring.mvc.hiddenmethod.filter.enabled: true

Basically, the filter that handles the _method request parameter is now disabled by default! So, even DELETE is sent as POST! And your controller must be having mapping only for DELETE (Which is correct btw). So, the error. ​

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.2-Release-Notes#httphiddenmethodfilter-disabled-by-default