Trying to pass a JavaScript object to Spring MVC. I am getting 400 bad request error. Tried all different approaches but still no luck. I have attached Controller, the object that I am passing in JS and Java and the response. Would appreciate any help. When I change @RequestBody to @ModelAttribute I receive the parameters in the controllers but the integers are zero and the arrays are empty.
Controller
@RequestMapping(value="/getInstrumentsByTypeMakeModel" , method=RequestMethod.GET)
public @ResponseBody List<Instrument> getInstrumentsByTypeMakeModel(@RequestBody SearchInstrument search) {
// do Something and create searchResult;
return searchResult;
}
JavaScript
var searchInstrument = {
operator: siteOperator,
companyId:loggedInUserCompanyId ,
isServiceProvider:isServiceProvider ,
types:selectedType,
makes:selectedMake,
models:selectedModel,
typeOthers:selectedTypeOther,
makeOthers:selectedMakeOther,
modelOthers:selectedModelOther
};
jQuery.ajax({
url:server_name+ "/getInstrumentsByTypeMakeModel",
type: "GET",
dataType: "JSON",
data:JSON.stringify(searchInstrument),
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
console.log(resultData);
},
error: function(jqXHR, status) {
// error handler
}
});
SearchInstrument Class
public class SearchInstrument {
public SearchInstrument(){
}
public SearchInstrument(int operator,int companyId, boolean isServiceProvider,
List<Integer> types,List<Integer> makes, List<Integer> models,
List<String> typeOthers,List<String> makeOthers,List<String> modelOthers
){
this.operator=operator;
this.companyId=companyId;
this.types=types;
this.makes=makes;
this.models=models;
this.typeOthers=typeOthers;
this.makeOthers=makeOthers;
this.modelOthers=modelOthers;
}
private int operator;
private int companyId;
private boolean isServiceProvider;
private List<Integer> types=new ArrayList<Integer> ();
private List<Integer> makes=new ArrayList<Integer> ();
private List<Integer> models=new ArrayList<Integer> ();
private List<String> typeOthers=new ArrayList<String> ();
private List<String> makeOthers=new ArrayList<String> ();
private List<String> modelOthers=new ArrayList<String> ();
//getters and setters
}
[This is the parameters][https://i.stack.imgur.com/2cxde.png]
[and this is the URL:][https://i.stack.imgur.com/BPMaS.png]