400 (Bad Request) when sending array to Spring Controller using AJAX

2
  

POST link 400 (Bad Request)

I'm getting this error when trying to send 2 array's to my Spring controller, however I'm getting this error, I tried some solutions I found in SOen but I did not succeed. The version of my Spring is 3.0.2.

JS Code

var url = '/app/workflow/execute/';
$.ajax({
    url: url,
    type: 'POST',
    dataType: 'json',
    data: {
        processName : pName,
        argumentsNameArray : argumentsNameArr,
        argumentsValArray : argumentsValArr
    },
})
.done(function(data) {
    console.log(data);
});

Java code

@RequestMapping(value = "/workflow/execute/", method = RequestMethod.POST)
@ResponseBody
public WorkflowProcess executeWFProccess(
        @PathVariable("processName") String processName,
        @PathVariable("argumentsNameArray[]") String[] argumentsNameArray,
        @PathVariable("argumentsValArray[]") String[] argumentsValArray) {

    WorkflowProcess wfProcess = new WorkflowProcess();
    ArrayList<WFArgument> wfArguments = new ArrayList<WFArgument>();
    wfProcess.setProcessName(processName);

    for (int i = 0; i < argumentsNameArray.length; i++) {
        wfArguments.add( new WFArgument(argumentsNameArray[i], argumentsValArray[i]) );
    }
    wfProcess.setArguments(wfArguments);

    return wfProcess;
}
    
asked by anonymous 08.10.2015 / 20:36

2 answers

1

First we have to understand how the request is made, how the values will arrive on the server. For this we just see in the jQuery documentation and we will have the%% of the ajax there. is sent as URL parameters.

The second thing to understand is the difference of data and @PathVaviable , that is, when each one should be used. From the documentation we can see that @RequestParam is used to retrieve variable values in URI templates , such as @PathVaviable , then we would have something like this:

@RequestMapping(value = "/workflow/execute/{processName}", method = RequestMethod.POST)
public WorkflowProcess executeWFProccess(@PathVaviable("processName") final String processName) {
    // faz alguma coisa
}

As we saw at the beginning your request puts the values as parameters and not as variables in the URL. So when we're using parameters we have to use %

Considering this, we only need to change from /workflow/execute/{processName} to @RequestParam in your example, thus:

@RestController
@RequestMapping(value = "/app")
public class WorkflowProcessWS {

    @RequestMapping(value = "/workflow/execute/", method = RequestMethod.POST)
    public WorkflowProcess executeWFProccess(@RequestParam("processName") final String processName,
            @RequestParam("argumentsNameArray[]") final String[] argumentsNameArray,
            @RequestParam("argumentsValArray[]") final String[] argumentsValArray) {
        final WorkflowProcess process = new WorkflowProcess();
        final List<WFArgument> wfArguments = new ArrayList<>();
        process.setProcessName(processName);
        for (int i = 0; i < argumentsNameArray.length; i++) {
            wfArguments.add(new WFArgument(argumentsNameArray[i], argumentsValArray[i]));
        }
        process.setArguments(wfArguments);
        return process;
    }

}

Just to test I made a call this way:

var url = '/app/workflow/execute/';
$.ajax({
    url : url,
    type : 'POST',
    dataType : 'json',
    data : {
        processName : "Nome do Processo",
        argumentsNameArray : ["Name1", "Name2", "Name3"],
        argumentsValArray : ["Val1", "Val2", "Val3"]
    },
}).done(function(data) {
    console.log(data);
    console.log("processName: " + data.processName);
    $.each(data.arguments, function(key, value) {
        console.log("key: " + key + " | value: " + value.val + " | name: " + value.name);
    });
});

And the return logged this (only a part, to get smaller here) in the chrome console:

Finally,ifnecessary,youcanretrievealloftherequestparametersbymappingthemtoa MultiValueMap , including something like this in your method:

@RequestParam final MultiValueMap<String, String> params
    
12.10.2015 / 04:47
0

Where does the argumentsNameArr and argumentsValArray come from in the Javascript part?

They must be in a format of type variavel[]=valor1 , variavel[]=valor2 to be interpreted by Java as an Array

Also try using

    @PathVariable("argumentsNameArray") String[] argumentsNameArray // repare na ausencia dos []
    
09.10.2015 / 01:05