Java Spring MVC 中不存在所需的字符串参数错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19619088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:50:41  来源:igfitidea点击:

Required String parameter is not present error in Spring MVC

javaajaxspringspring-mvc

提问by nabiullinas

I try to make an AJAX query to my controller in Spring MVC.

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

My action code is:

我的操作代码是:

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

My Ajax query is:

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

But I got an error:

但我得到了一个错误:

Required String parameter ''start_date is not present

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

Why? As I know I presented it like (@RequestParam(value = "start_date") String start_date

为什么?据我所知,我提出了它(@RequestParam(value = "start_date") String start_date

UDP
Now I give 404 My class to take data

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

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

My js AJAX call is:

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

And controller action:

和控制器动作:

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

And JSON data is:

JSON 数据为:

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

采纳答案by gadget

On the server side you expect your request parameters as query strings but on client side you send a json object. To bind a json you will need to create a single class holding all your parameters and use the @RequestBody annotation instead of @RequestParam.

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

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

Here isa more detailed explanation.

这里有更详细的解释。

回答by User-8017771

I had the same issue.. I solved it by specifying the config params in the post request:

我有同样的问题..我通过在 post 请求中指定配置参数来解决它:

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

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

configwas the parameter I included and it started working.. Hope it helps :)

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

回答by Somesh Gupta

Spring Boot Code

春季启动代码

@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 Request Link to be send:Add the parameters and value using Parmas in postman and see the below request link.

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

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