【问题标题】:Not able to receive parameters using Postmapping in Rest Controller无法在 Rest Controller 中使用 Postmapping 接收参数
【发布时间】:2018-03-24 00:03:55
【问题描述】:

我的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);
}

输出是

要关闭的事务:null,序列号:null

我在这里缺少什么?

【问题讨论】:

    标签: java ajax spring spring-restcontroller


    【解决方案1】:

    您将数据作为请求正文发布,当然使用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 格式相同。

    【讨论】:

    • 你的意思是我的 ResultDto 应该有 txToClose & sno?
    • 是的,如果您不想将 JSON 映射到 ResultDto,可以查看此讨论:stackoverflow.com/a/12897632/5852226
    • 当我将我的签名设为 public ResultDto txToClose(HttpServletRequest request, HttpServletResponse response,@RequestBody ObjectNode json) 时,它起作用了
    【解决方案2】:

    删除线

    contentType : "text/plain; charset=utf-8",

    或使用默认内容类型

    contentType : "application/x-www-form-urlencoded; charset=utf-8",

    它应该可以工作。

    【讨论】:

    • 只是为了调试,用 @RequestMapping(value="/txToClose") 尝试一次 @ResponseBody public ResultDto txToClose(HttpServletRequest request, HttpServletResponse response) throws BBException { logger.info("Called txToClose controller") ; ResultDto resultDto = new ResultDto();字符串 txToClose=request.getParameter("txToClose");字符串 sno=request.getParameter("sno"); logger.info("要关闭的交易:"+txToClose+", 序列号:"+sno); }
    • 解决了。通过添加,@RequestBody ObjectNode json
    【解决方案3】:

    我通过在签名中添加 @RequestBody ObjectNode json 解决了这个问题。

    public ResultDto txToClose(HttpServletRequest request, HttpServletResponse response,@RequestBody ObjectNode json) throws BBException
    {
        logger.info("Called txToClosecontroller");
        ResultDto result = new ResultDto();
    
        String txToClose= json.get("txToClose").asText();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-27
      • 1970-01-01
      • 2018-02-28
      • 2021-08-18
      • 2019-08-10
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 2021-06-12
      相关资源
      最近更新 更多