带RequestBody的POST请求不起作用 [英] POST request with RequestBody is not working

查看:571
本文介绍了带RequestBody的POST请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我正在使用POST和GET方法. post是初始化变量,而GET是获得变量. 我用邮递员发出请求.

in the below example, i am using POST and GET methods. post is to initialize a varibale and GET is to get this varibale. I use Postman to make the requests.

我收到错误

@RequestBody(value = "val") //cant resolve method value

请让我知道如何解决belwo错误,以便我可以使用post方法进行初始化并获取方法来检索值

please let me know how to fix the belwo error so i can use post method for initialization and get methdo to retrieve the value

Controller1

@Controller
@RequestMapping("/call1")
public class Call1 {

public String str = "inti";

@RequestMapping(value = "/intiparam1", method = RequestMethod.POST)
public void intiParam1(@RequestBody(value = "val") String val) {
    this.str = val;
}

@RequestMapping(value = "/getparam1", method = RequestMethod.GET)
public String getParam1() {
    return this.str;
}
}

推荐答案

创建类变量并在控制器中使用其他代码.

Create a class Variable and use other code in controller.

class Variable {

String data= 'val';
}

@RequestMapping(value = "/intiparam1", method = RequestMethod.POST)
public void intiParam1(@RequestBody Variable val) {
    this.str = val.data;
}

发出请求时,将json作为{"data":"12345"}

When making a request pass json as {"data":"12345"}

,然后在代码中使用@RequestBody变量v代替String,因为它将满足您的默认值需求,并使代码可扩展,因为您将来可以根据需要在现有变量中添加不同的属性.

and then use @RequestBody Variable v in code instead of String as it will serve your purpose of default value and will make the code extensible as you can add different properties to the existing variable in future if needed.

这篇关于带RequestBody的POST请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆