【问题标题】:Required String parameter is not present error in Spring MVCSpring MVC 中不存在必需的字符串参数错误
【发布时间】:2013-11-06 07:09:17
【问题描述】:

我尝试在 Spring MVC 中对我的控制器进行 AJAX 查询。

我的操作代码是:

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){
    //some code    
}

我的 Ajax 查询是:

$.ajax({
        type: "POST",
        url:url,
        contentType: "application/json",
        data:     {
                start_date:   scheduler.getEvent(id).start_date,
                end_date:  scheduler.getEvent(id).end_date,
                text: scheduler.getEvent(id).text,
                userId: userId
        },
        success:function(result){
         //here some code
        }
    });

但我得到一个错误:

必需的字符串参数''start_date 不存在

为什么? 据我所知,我将其介绍为 (@RequestParam(value = "start_date") String start_date

UDP
现在我给 404 我的班级取数据

public class EventData {
    public String end_date;
    public String start_date;
    public String text;
    public String userId;
    //Getters and setters
}

我的 js AJAX 调用是:

$.ajax({
    type: "POST",
    url:url,
    contentType: "application/json",
    // data: eventData,
    processData: false,
    data:    JSON.stringify({
        "start_date":   scheduler.getEventStartDate(id),
        "end_date":  scheduler.getEventEndDate(id),
        "text": scheduler.getEventText(id),
        "userId": "1"
    }),

和控制器动作:

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestBody EventData eventData){    
}

而 JSON 数据是:

end_date: "2013-10-03T20:05:00.000Z"
start_date: "2013-10-03T20:00:00.000Z"
text: "gfsgsdgs"
userId: "1"

【问题讨论】:

  • jquery 是否将data 元素序列化为请求参数?作为 url-encoded-form 参数?
  • 只是JS文件中的变量
  • 在您的两个示例中,您是否尝试过实际检查您的 POST 请求并确保它传递了所需的值?在每种情况下都显示了什么?

标签: java ajax spring spring-mvc


【解决方案1】:

在服务器端,您希望您的请求参数作为查询字符串,但在客户端,您发送一个 json 对象。要绑定 json,您需要创建一个包含所有参数的类,并使用 @RequestBody 注释而不是 @RequestParam。

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestBody CommandBean commandBean){
    //some code
}

Here is更详细的解释。

【讨论】:

  • 您能发布您尝试发送的实际 JSON 吗?您可以使用 Firebug 或 Chrome/Chromium 捕获它。
  • 你能写吗,我如何在谷歌浏览器中看到它?
  • 在请求负载中显示 start_date=Tue+Oct+01+2013+00%3A00%3A00+GMT%2B0400+(Russian+Standard+Time)&end_date=Tue+Oct+01+2013+00 %3A05%3A00+GMT%2B0400+(俄语+标准+时间)&text=New+event&userId=
  • 这很奇怪,似乎 jquery 默认将数据作为查询字符串发送。这在 POST 请求的情况下不好。尝试在您的 .ajax 请求中添加 processData: false 标志,并将您的数据元素包装在 JSON.stringify({...}) 调用中。喜欢:pastebin.com/JLKgLivC 在 chrome 中查看请求有效负载:工具 > 开发人员工具 > 网络选项卡
  • 我发现了一个问题。它在控制器的返回值中。我没有设置 JSP 页面,它总是返回 404。你的答案是正确的
【解决方案2】:

我遇到了同样的问题。我通过在发布请求中指定配置参数解决了它:

var config = {
    transformRequest : angular.identity,
    headers: { "Content-Type": undefined }
}

$http.post('/getAllData', inputData, *config*).success(function(data,status) {
    $scope.loader.loading = false;
})

config 是我包含的参数,它开始工作.. 希望对你有帮助:)

【讨论】:

    【解决方案3】:

    Spring 引导代码

    @RequestMapping(value = "events/add", method = RequestMethod.POST)
    public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){
        //some code    
    }
    

    要发送的 Postman 请求链接: 在 postman 中使用 Parmas 添加参数和值,并查看以下请求链接。

    http://localhost:8080/events/add?start_date=*someValue*&end_date=*someValue*&text=*someValue*&userId=*someValue*
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 2019-09-23
      • 2021-04-01
      • 1970-01-01
      • 2015-01-21
      • 2018-05-03
      相关资源
      最近更新 更多