0

I have a POJO as follows, it contains a list of other POJOs:

public class Commit {

    private long revision;
    private Date date;
    private String author;
    private String comment;
    private String akuiteo;
    private List<ChangedPath> changedPathList = new ArrayList<ChangedPath>();

//Getters, Setters and Constructor following
}

My controller expects 3 parameters one of them being an array or a list of Commit:

@RequestMapping(value="/selectedCommits",method=RequestMethod.POST)
@ResponseBody
public List<Commit> getAllDependentCommits(@RequestParam("branch")String branch,
@RequestParam("tag")String tagName,@RequestParam(value="commits[]") Commit[] commits) throws IOException{
    List<String> changedPaths=new ArrayList<String>();
    List<Commit> dependentCommits=UserService.listAllCommits(branch, tagName, commits);
    //UserService.createPatchFromSelectedCommits(branch, tagName, revisions);
    return dependentCommits;
}

I have also tried:List<Commit> commits instead of Commit[ ] commits

I call the controller from a jQuery script using AJAX:

$("#buttonCommit").click(function(e){
console.log(branch);
console.log(tag);
console.log(selectedCommits);
$.ajax({
    url:'selectedCommits?branch='+branch+'&tag='+tag,
    method: 'POST',
    dataType: 'application/json',
    contentType: 'application/json; charset=utf-8',
    data:{
        commits:selectedCommits,
    },
    success:function(data){
        alert('wow smth really happened, here is the response : '+data[1]);
        window.selectedCommits=selectedCommits;
        window.dependentCommits=data.dependentCommits;
        console.log(data.dependentCommits);
    }
})
});

I have also tried: commits:JSON.stringify(selectedCommits)

Every time I get the error:

org.springframework.web.bind.MissingServletRequestParameterException:
Required Commit[] parameter 'commits[]' is not present

I have also tested passing an array of Long representing the revisions and it worked, I can manage using it for my services but it would be nicer to have an array of objects. What am I doing wrong?

3 Answers 3

7

You should use

@RequestBody List<Commit> commits

in your controller instead of

@RequestParam(value="commits[]") Commit[] commits
Sign up to request clarification or add additional context in comments.

13 Comments

If I use a RequestBody will I have to create an object that holds a branch a tag and a commit list so that I can retrieve all 3 parameters?
@dannemp no, you don't have to. just replace - and that's all. @RequestParam is used to get query parameter from URL and the @RequestBody is used to map request body - everything is simple:)
@dannemp you can move tagName and branch to your url as query parameters. it is not the best practice here. but if you just want to make it work - this is the simplest way
Now I get "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported" as error
@dannemp try to use contentType: "application/json" in your ajax request
|
0

In my project what I did is the following (I'll try to adapt my solution to your code):

$("#buttonCommit").click(function(e){
//Populate the array with selectedCommits values
var myArray = new Array();

$.ajax({
    url:'selectedCommits',
    method: 'POST',
    dataType: 'json',
    data:{
        branch:branch,
        tag:tag,
        commits:myArray,
    },
    success:function(data){
        alert('wow smth really happened, here is the response : '+data[1]);
        window.selectedCommits=selectedCommits;
        window.dependentCommits=data.dependentCommits;
        console.log(data.dependentCommits);
    }
})
});

By doing in this way and without using the JS stringify method all works pretty good

Angelo

1 Comment

I have tried your solution and it doesn't solve the problem. I log both the selectedCommits and myArray to the console and they are exactly the same.
0

JS:

 $.ajax({
        type : "post",
        dataType : 'json', 
        url : 'addAdmin',  
        data : JSON.stringify(selectedCommits)
 })

Controller (note it takes a custom String only). Then it uses Jackson to parse the string manually, because Spring won't do it in this case. (Spring will only auto-parse if you're using @ModelAttribute form binding.)

@PostMapping("/addAdmin")
public boolean addAdmin(@RequestBody String json) throws Exception {

      String decodedJson = java.net.URLDecoder.decode(json, "UTF-8");
      ObjectMapper jacksonObjectMapper = new ObjectMapper(); // This is Jackson
      List<UserRolesGUIBean> userRolesGUIBeans =  jacksonObjectMapper.readValue(
              decodedJson, new TypeReference<List<UserRolesGUIBean>>(){});
      // Now I have my list of beans populated.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.