0

Request method 'GET' not supported, Method Not Allowed 405()

当我在我的控制器类中使用@DeleteMapping 时,我得到了上述类型的错误首先我没有在我的DeleteMapping 中使用“路径”我直接提供了我的路径,然后我得到了同样的错误为什么会出现这种类型的错误。请提及此类错误的主要原因是什么以及解决方案 Controller.java 是什么

package com.main.AngBoot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import com.main.AngBoot.bean.Product;
import com.main.AngBoot.service.ProductHardcodedService;

@CrossOrigin(origins="http://localhost:4200")
@RestController
public class ProductController {
    @Autowired
    private ProductHardcodedService prodService;
    @GetMapping("/users/{productname}/prodct")
    public List<Product> getAllProducts(@PathVariable String productname){
        return prodService.findAll();
        
    }
    @DeleteMapping(path="/users/{productname}/prodct/{id}")
    public ResponseEntity<Void> deleteProduct(@PathVariable String productname,
            @PathVariable long id){
        Product product = prodService.deleteById(id);
        if (product != null) {
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.notFound().build();
    }

}

我在我的 chrome 浏览器中收到此错误 在此处输入图像描述

我在邮递员上遇到了同样的错误 在此处输入图像描述

4

1 回答 1

2
@DeleteMapping(path="/users/{productname}/prodct/{id}")

在这里,您使用的是@DeleteMapping,这意味着您应该HTTP DELETE为此端点发送请求。你得到Method Not Allowed是因为你正在发送一个HTTP GET你不允许做的请求,因为你没有@DeleteMappingHTTP GET消息映射方法注释。

于 2020-08-21T08:47:50.257 回答