0

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]

MJ82
  • 1
  • 3
  • what is the stacktrace in the API? Is there any error ? – Christos Karapapas Apr 07 '19 at 07:00
  • @Christos K If I use RequestBody it doesn't even hit the Controller, but when I change it to ModelAttribute like this the parameters are received but the arrays are empty and the integers are zero – MJ82 Apr 07 '19 at 18:15

1 Answers1

0

I'm not entirely sure, but it just doesn't seem the best idea to combine a GET request and try to pass an object, that is why you see your searchController JS object at the url, in the second picture.

The following question has a more thorough explanation.

I think you should try to convert it to a POST request.

Alternatively if you still want to keep it as GET for your reasons, you should pass your object as parameters in the URL and use @RequestParam in your controller.

Christos Karapapas
  • 1,018
  • 3
  • 19
  • 40
  • 1
    I changed it to POST and it worked. It is strange because I tried POST before but probably with RequestParam not RequestBody. Thanks a lot appreciate it. – MJ82 Apr 07 '19 at 19:13
  • I also changed the SearchController class name to SearchInstrument to avoid similarity with a controller class. – MJ82 Apr 07 '19 at 20:42
  • Another note is that SearchInstrument cannot be an inner class of the controller, unless it is static. If you have it as an inner class and not static you will get "No default constructor" – MJ82 Apr 08 '19 at 15:48