Spring I';I’我想在邮递员中授权,但它给了我;请求方法';获取';“不支持”;

Spring I';I’我想在邮递员中授权,但它给了我;请求方法';获取';“不支持”;,spring,spring-boot,postman,Spring,Spring Boot,Postman,我首先尝试在“/login”中输出POST方法的日期,因为我不确定代码的正确性。我希望你能帮助我,谢谢 MainController.java @RestController public class MainController { @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(@RequestBody Credentials credentials) {

我首先尝试在“/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'不受支持”

您应该从方法下拉列表中选择“POST”方法:


你能提供一张邮递员的截图吗?因为我认为你没有从URL旁边的下拉列表中选择“POST”。默认情况下,它应该是GET(获取)

Try request body x-www-form-urlencoded

,如回答中所述,从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;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseBody;
导入org.springframework.web.bind.annotation.RestController;
@RestController
公共类主控制器{
@RequestMapping(value=“/login”,method=RequestMethod.POST)
公共字符串登录(@RequestBody凭据){
返回“username:+credentials.getUsername()+”密码:+credentials.getPassword();
}
}
公共类凭据{
私有字符串用户名;
私有字符串密码;
私人证书(){
}
//省略了getter和setter,请确保您拥有它们。
}
  • @RequestBody
    注释需要一个JSON对象进行反序列化。必须有一个对象可用于映射
  • 由于您使用的是
    @RestController
    注释,因此不需要在方法上方使用
    @ResponseBody
    。已经是了

由于某些原因,在MainController前面必须有“@RequestMapping()”,这对我来说很有效
或者提到请求正文x-www-form-urlencoded

我已经选择了POST,它没有改变任何事情我已经选择了POST,它没有改变任何事情相同的错误“请求方法'GET'不受支持”我已经更新了我的代码,并提供了上面我的邮差的屏幕截图。我还发现这个错误对于Spring Boot的用户来说是很常见的,并且有代码如何避免错误“method={RequestMethod.POST,RequestMethod.GET}”,之后这个错误消失了,但是出现了新的错误“Required request body丢失:public java.lang.String com.example.demo.controller.MainController.login”(com.example.demo.model.GlobalVar.Credentials)看起来您正在参数中引用Credentials类,但在此过程中实现了一些GlobalVar。请根据您的实现在方法参数中修复它。错误将消失。如果您的目标是创建一个简单的应用程序,该应用程序将JSON对象作为参数并返回响应。仅包括在pom.xml中spring启动web dependency,然后继续下面给出的答案。如果您想进一步创建登录模块,那么spring security是开始您的旅程的一个好方法。我建议您从初学者教程开始。有很多很棒的教程,例如来自Koushik Kothagal的