Spring Boot 尝试访问 Post 请求 URL,但显示 GET 不支持

Posted

技术标签:

【中文标题】Spring Boot 尝试访问 Post 请求 URL,但显示 GET 不支持【英文标题】:Spring Boot Tries to Access A Post Request URL but shows GET not supported 【发布时间】:2019-12-08 11:44:19 【问题描述】:

我今天刚开始学习 Spring Boot,我想为我的 Spring Boot 项目创建一个 GET/POST 请求。当我尝试访问具有发布请求的 URL 时,它显示 405 错误,指出“不支持请求方法 'GET'”。

我认为我的 POST 请求代码有问题,但我不知道我哪里做错了。我试图搜索一个教如何编写正确的 GET/POST 请求的教程,所以我找不到任何好的东西。

如果你们有任何好的网站来教授 Spring Boot 中的基本 HTTP 请求,那就太好了。我试图在 *** 上找到答案,但没有找到任何答案。

我一直使用的Spring Boot项目来自Spring.io官网:https://spring.io/guides/gs/rest-service/

我想为我的项目调用 POST 请求,以便更好地了解 HTTP。

这里是控制器的源代码: 打包你好;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.*;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
public class GreetingController 
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    // GET Request
    @RequestMapping(value="/greeting", method = GET)
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) 
        return new Greeting(counter.incrementAndGet(), name);

    

    @RequestMapping(value = "/testpost", method = RequestMethod.POST)
    public String testpost() 
        return "ok";
    


这是应用程序的源代码:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application 
    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

这里是 Greeting Object 的源代码 打包你好;

public class Greeting 

    private final long id;
    private final String content;

    public Greeting(long id, String content) 
        this.id = id;
        this.content = content;
    

    public long getId() 
        return id;
    

    public String getContent() 
        return content;
    

我可以使用“/greeting” URL 使 GET 请求正常工作。 我试图访问“/testpost” url,但它显示 405 错误,即不支持 GET 方法。

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported

【问题讨论】:

你是如何请求“testpost”的?来自邮递员或类似“/greeting”的浏览器 我尝试直接在浏览器中打开访问localhost:8080/testpost。 那个问题。您需要通过 Postman 或任何其他软件进行发布请求。签出这个答案。 ***.com/a/57282858/3204335 【参考方案1】:

如果您尝试通过直接在浏览器中打开来打开http://localhost:8080/testpost,它将无法正常工作,因为在浏览器中打开会发出 GET 请求。

我不确定您是如何尝试发送帖子请求的,我尝试从邮递员发出相同的帖子请求并且能够得到响应。下面是截图。

【讨论】:

【参考方案2】:

您似乎正试图直接从网络浏览器发出帖子请求,但这是行不通的。 当您直接从 Web 浏览器地址栏中点击 URL 时,它被视为 GET 请求。因为在您的情况下,没有 GET API 作为 /testpost ,所以它给出了错误。

尝试使用Postmancurl 命令 等rest 客户端发出post 请求。

我用邮递员尝试了你的帖子端点,它工作正常。 PFA 快照供您参考。

希望这会有所帮助。

【讨论】:

【参考方案3】:

从您尝试 POST 请求的位置。如果您从浏览器窗口调用 POST 调用,那么它将不起作用,浏览器将只发送 GET 请求。您是否从邮递员或 UI 方面尝试过。它会起作用的。

【讨论】:

以上是关于Spring Boot 尝试访问 Post 请求 URL,但显示 GET 不支持的主要内容,如果未能解决你的问题,请参考以下文章

发送 post 请求时出现 CORS 错误,但在 Spring Boot 和 Reactjs 中未获取请求

Spring boot - 一些 POST 请求严重延迟

Spring Boot Cors 问题:尝试了所有方法,但 POST 不起作用

在 Spring Boot 中从 Angular 向后端发送 post 请求中的对象的问题

如何使用 Postman 表单数据将 POST 请求发送到 Spring Boot REST API?

Angular 11 - Spring Boot - MySQL:POST请求未成功创建数据库条目