【问题标题】:I'm trying to authorize in PostMan, but it gives me "Request method 'GET' not supported"我正在尝试在 PostMan 中授权,但它给了我“不支持请求方法 'GET'”
【发布时间】:2020-07-09 01:17:38
【问题描述】:

我首先尝试在 '/login' 中输出 POST 方法的日期,因为我不确定我的代码的正确性。我希望你能帮助我,谢谢。

MainController.java

@RestController
public class MainController {
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody Credentials credentials) {
        return "username: " + credentials.getUsername() + " password: " + credentials.getPassword();
    }
}

邮递员查询

[![Postman dropdown list][1]][1]

[Screenshot link, if there is no picture above][1]

{
    "username": "admin",
    "password": "admin"
}

我尝试将日期作为一行 (JSON) 和表单发送,但无论如何它给了我这些错误

{
    "timestamp": "2020-03-29T10:03:20.711+0000",
    "status": 405,
    "error": "Method Not Allowed",
    "message": "Request method 'GET' not supported",
    "path": "/login"
}

我注意到,在编译器中向我抛出了这个错误 "org.springframework.security.web.firewall.RequestRejectedException:请求被拒绝,因为 URL 包含潜在的恶意字符串“;” ”,之后给了我这个“不支持请求方法'GET'”

【问题讨论】:

  • 如果您的目标是创建一个简单的应用程序,该应用程序将 JSON 对象作为参数并返回响应。在 pom.xml 中仅包含 spring boot web 依赖项,然后继续下面给出的答案。如果您想更进一步并创建一个登录模块,那么 spring-security 是开始您的旅程的好方法。我建议您从初学者教程开始。有很多很棒的,比如来自 Koushik Kothagal 的this

标签: spring spring-boot postman


【解决方案1】:

您应该从 METHOD 下拉菜单中选择“POST”方法:

【讨论】:

  • 我选择了 POST,它不会改变任何东西
【解决方案2】:

你能提供邮递员的截图吗?因为我认为您没有从 URL 旁边的下拉列表中选择“POST”。默认应该是 GET

【讨论】:

  • 我选择了 POST,它不会改变任何东西
【解决方案3】:

尝试请求正文 x-www-form-urlencoded

【讨论】:

  • 同样的错误“不支持请求方法'GET'”
【解决方案4】:

如答案中所述,从 Postman 的下拉列表中选择 POST 方法将有助于解决以下错误:

"Request method 'GET' not supported."

那么你将面临以下错误:

{
    "timestamp": "2020-03-28T16:54:55.288+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Required request body is missing: public java.lang.String com.example.demo.controller.MainController.login(java.lang.String,java.lang.String)",
    "path": "/login"
}

要解决这个问题,你应该稍微修改一下端点:

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody Credentials credentials) {
        return "username: " + credentials.getUsername() + " password: " + credentials.getPassword();
    }
}
public class Credentials {

    private String username;
    private String password;

    private Credentials() {
    }

    // getters and setters omitted, make sure you have them.
}
  • @RequestBody 注释需要一个 JSON 对象反序列化。必须有一个可用于映射的对象。
  • 因为你使用的是@RestController注解,所以不需要@ResponseBody上面的方法。已经是included

【讨论】:

  • 我已经更新了我的代码并提供了上面邮递员的截图。我还发现这个错误对于 Spring Boot 的用户来说很常见,并且有代码如何避免错误“method = {RequestMethod.POST,RequestMethod.GET}”,之后这个错误消失了,但是有新的错误“缺少所需的请求正文:public java.lang.String com.example.demo.controller.MainController.login(com.example.demo.model.GlobalVar.Credentials)"
  • 看起来您在参数中引用了 Credentials 类,但在此过程中已经实现了一些 GlobalVar。根据您的实现在方法参数中修复它。错误将消失。
【解决方案5】:

由于某些原因,MainController 前面一定有“@RequestMapping()”,它对我有用

【讨论】:

    【解决方案6】:

    试试@RequestBody(required=false) 或提到的请求正文 x-www-form-urlencoded

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 2017-09-20
      • 2021-07-25
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2020-08-14
      相关资源
      最近更新 更多