gpt4 book ai didi

java - 无法在 Rest Controller 中使用 Postmapping 接收参数

转载 作者:行者123 更新时间:2023-12-02 12:08:49 25 4
gpt4 key购买 nike

我的 Ajax 代码如下所示

$.ajax({
type : 'post',
url : url,
async : false,
data: {'txToClose': '1234,5678','sno':'0195'},
contentType : "application/x-www-form-urlencoded; charset=utf-8",
dataType : "json",
success : function(result,status,xhr){
console.log(result);

}
});

txToClose 值 1234,5678 将以逗号分隔的字符串形式来自文本框。用户将以逗号分隔的形式键入它们。

我正在尝试在 Controller 中接收它们作为

@PostMapping("/txToClose")  
public ResultDto txToClose(HttpServletRequest request, HttpServletResponse response) throws BBException
{
logger.info("Called txToClose controller");
ResultDto resultDto = new ResultDto();

String txToClose= request.getParameter("txToClose");
String sno= request.getParameter("sno");

logger.info("Transactions to close :"+txToClose+", Serial Num :"+sno);
}

输出是

Transactions to close :null, Serial Num :null

我在这里缺少什么?

最佳答案

您将数据作为请求正文发布,当然使用 request.getParameter() 会给您 null。 getParameter() 用于从 URL 参数中检索值。

我对 SpringMVC 不太有经验,但尝试将你的方法参数更改为

public ResultDto txToClose(@RequestBody ResultDto resultDto ) throws BBException
{
logger.info("Called txToClose controller");

String txToClose= resultDto.getTxtToClose();
String sno= resultDto.getSno();

logger.info("Transactions to close :"+txToClose+", Serial Num :"+sno);
}

我假设您的 ResultDto 与您的 JSON 格式相同。

关于java - 无法在 Rest Controller 中使用 Postmapping 接收参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46703025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com