【问题标题】:Request method 'GET' not supported,Method Not Allowed, status=405不支持请求方法“GET”,不允许方法,状态=405
【发布时间】:2020-08-21 08:37:43
【问题描述】:

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 浏览器出现此错误

我在邮递员上遇到了同样的错误

【问题讨论】:

  • 好吧,因为您不应该向 @DeleteMapping 端点发送 HTTP GET 消息。另外,请不要将您自己的文本包含为citation
  • 邮递员错误不同。正确配置你的 cors。

标签: spring-boot


【解决方案1】:
@DeleteMapping(path="/users/{productname}/prodct/{id}")

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

【讨论】:

  • 请告诉我如何解决这种类型的错误,在哪里更改我的代码,我不明白,因为我是 web 服务的新手
  • 发送 HTTP 删除请求而不是获取请求
  • 如何发送删除请求而不是获取请求,我在另一个服务类中创建删除服务
  • @nilil 你是如何达到你的端点的?像邮递员一样还是被其他服务消费?
  • here
猜你喜欢
  • 2015-04-23
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
  • 2016-05-24
相关资源
最近更新 更多