我正在尝试使用POST方法使用AJAX将两个参数发送到我的Spring REST控制器。但是这些参数在我的控制器中显示为null。请找到代码,如果我遗漏任何东西,请告诉我。

var formData = {
  txToClose: '1234,5678,98549',
  sno:  '0195'
};

$.ajax({
  type: 'post',
  url: url,
  async: false,
  data: JSON.stringify(formData),
  contentType: "application/json",
  dataType: "json",
  success: function(result, status, xhr) {
    console.log(result);
  }
});


@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);
}

最佳答案

创建这样的类:

    class Myclass{
          private String  txToClose;
             private String    sno;
 // getters setters
    }


并在您的方法是这样的:

@PostMapping("/txToClose")
public ResultDto txToClose(@RequestBody Myclass class ) throws BBException
{
   // your logic
}

10-08 04:21