简体   繁体   中英

Bad request on Posting Nested JSON to Spring Controller

I am trying to POST a json to a Spring MVC Controller but it i always giving me Bad request.

The jquery code is

 $.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');
                                }
                        });

The Object Class code is

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

}

And the Controller is

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

If I remove the bearertoken part it works well. What I am doing wrong here. If I try to print the body it is a Valid json

You send bearerToken but the RequestWrapper has an accessToken property. The setter has a typo, btw.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM