Deserializing json array in Spring MVC controller

后端 未结 3 1152
滥情空心
滥情空心 2021-01-19 14:33

I am sending a list of json object and trying to deserialize it in my Spring controller. But all the time I am getting error of \'Bad request\' and results into a status co

相关标签:
3条回答
  • 2021-01-19 14:50

    As @shazin is saying, you've most likely posted a wrong method to your question, or if not, simply make a change that he suggested.

    You'll need another fix, and that is renaming the usersList property from UserWrapper to users so that it matches the JSON property being sent.

    We these fixes, you should not have further problems, since your request is OK.

    0 讨论(0)
  • 2021-01-19 15:04

    I think you need to change your @RequestBody POJO to UserWrapper

    @RequestMapping(value="/task/add", method=RequestMethod.POST, 
            produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public void createTeamMember(@RequestBody UserWrapper userWrapper) {
        // Code to create members
    }
    
    0 讨论(0)
  • 2021-01-19 15:06

    I will not suspect server side binding error yet because you are getting 415 - Unsupported Media Type error. You are setting correct media type on controller and hence server side is looking good.

    At client side, Please make sure you are using jquery 1.5+ to make sure beforeSend() method is getting invoked.

    Easier way to content type will be,

    $.ajax({
            url:api,
            ....
            contentType: "application/json"
        });
    

    Please inspect network request from browser and make sure content type is set in header.

    If you receive 400-Bad Request, then you can start looking deserializing issues.

    0 讨论(0)
提交回复
热议问题