将嵌套JSON发布到Spring Controller的错误请求

时间:2014-10-14 09:57:48

标签: jquery jsp spring-mvc

我正在尝试将json发布到Spring MVC控制器,但它总是给我错误的请求。

jquery代码是

 $.ajax({
                            type: "POST",
                            url: "http://localhost:8080/Services/placeorderMultipleImages",
                            data:JSON.stringify({
                                bearerToken:access_token, 
                                images:[
                                      {
                                        imagePath:"C:\\Users\\XYZ\\Desktop\\test.jpg",
                                        imageInstruction:"Color Correction"
                                      },
                                      {
                                        imagePath:"C:\\Users\\XYZ\\Desktop\\Untitled-1.jpg",
                                        imageInstruction:"Brightness and contrast"
                                      }
                                    ] 
                                  }),
                            contentType: 'application/json',
                            mimeType: 'application/json',
                            success: function(data, textStatus ){
                                    console.log(data);
                                    alert("success");
                                },
                                error: function(data, textStatus){
                                    var obj =  data;
                                    console.log(data);
                                    alert('request failed');
                                }
                        });

对象类代码是

import java.util.List;

public class RequestWrapper {

    private String bearerToken;
    private List<ImageInstructionPair> images;

    public void setAccesToken(String inAccessToken)
    {
        this.bearerToken = inAccessToken;
    }

    public String getAccessToken()
    {
        return this.bearerToken;
    }

    public void setImages(List<ImageInstructionPair> inImages)
    {
        this.images = inImages;
    }

    public List<ImageInstructionPair> getImages()
    {
        return this.images;
    }

}

控制器

@RequestMapping(value = "/placeorderMultipleImages", method = RequestMethod.POST, consumes="application/json")
    public void  updateWithMultipleObjects(
            @RequestBody RequestWrapper requestWrapper) {

        List<ImageInstructionPair> images = requestWrapper.getImages();
        for(ImageInstructionPair image : images)
        {
            System.out.println(image.getImagePath() + " " + image.getImageInstruction());
        }
        //System.out.println(requestWrapper.getAccessToken());
        // TODO: call persistence layer to update
        return ;
    }

如果我删除bearertoken部分它运作良好。我在这做错了什么。如果我尝试打印身体,那就是有效的json

1 个答案:

答案 0 :(得分:1)

您发送bearerTokenRequestWrapperaccessToken属性。二传手有一个错字,顺便说一句。