SpringMVC Returning a List Of Objects With JSON
19,410
Solution 1
Firstly please make sure your Spring version is 3.1.1 release and you have added jackson.jar in you lib, then try using below code, your code has something reduntant.
@RequestMapping(value="/getCrimeTypeList.htm", method = RequestMethod.GET)
public @ResponseBody List<CrimeType> getCrimeTypeList(@RequestParam(value="crimeCatId") Integer crimeCatId) throws Exception{
try {
return this.crimeTypeManager.getCrimeTypeList(crimeCatId);
//return "true";
} catch (Exception e) {
logger.error(e.getMessage());
return null;
}
}
$("select#offenceCatId").change(function(){
var param={crimeCatId:$(this).val()};
$.ajax({
type:'GET',
url:'getCrimeTypeList.htm',
data:param,
success:function(data){
//append options to list
}
});
});
Solution 2
Your controller is expecting an request header Accept=application/json
, in your case you are not setting it.
Try setting the Accept
header
jQuery.ajax({
type:'GET',
url:'getCrimeTypeList.htm',
data:{crimeCatId:$(this).val()},
processData:false,
headers: {
Accept: 'application/json'
},
dataType: 'json',
success:function(data){
//append options to list
}
});
Author by
devdar
Updated on June 07, 2022Comments
-
devdar almost 2 years
I have two select options and i would like when the user selects one option that other is populated with data from the database. However i am having issues in getting the object list returned to the view.
Controller
@RequestMapping(value="getCrimeTypeList.htm", method = RequestMethod.GET) public @ResponseBody List<CrimeType> getCrimeTypeList(@RequestParam(value="crimeCatId") Integer crimeCatId) throws Exception{ try { List<CrimeType> crimeTypeList = this.crimeTypeManager.getCrimeTypeList(crimeCatId); return crimeTypeList; } catch (Exception e) { logger.error(e.getMessage()); return null; } }
JQuery
$("select#offenceCatId").change(function(){ $.ajax({ type:'GET', url:'getCrimeTypeList.htm', data:{crimeCatId: $(this).val()}, headers: { Accept: 'application/json' }, dataType: 'json', success:function(data){ alert('it worked'); } }); });
HTML
<li> <label>Offence Type</label> <form:select path="offenceTypeId" id="offenceTypeId" title="Offence Type"> <form:options items="${crimeType.crimeTypeList}" itemValue="crimeTypeId" itemLabel="crimeTypeDesc"/> </form:select> <form:errors path="offenceTypeId" class="errors" /> </li>
Error
"NetworkError: 400 Bad Request - http://localhost:8084/crimeTrack/getCrimeTypeList.htm?[object%20Object]"
EDITED I did some experimentation and found if the Controller is returning a String it works however once its returning an Object i am having the issues stated.
FireBug
GET http://localhost:8084/crimeTrack/getCrimeTypeList.htm?crimeCatId=6 406 Not Acceptable Response Headers Content-Length 1067 Content-Type text/html;charset=utf-8 Date Fri, 29 Mar 2013 00:58:17 GMT Server Apache-Coyote/1.1 Request Headers Accept application/json Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Host localhost:8084 Referer http://localhost:8084/crimeTrack/crime_registration.htm User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0 X-Requested-With XMLHttpRequest
-
devdar about 11 yearsI am getting the following error "NetworkError: 400 Bad Request - localhost:8084/crimeTrack/…"
-
devdar about 11 yearsI am getting the following error : ""NetworkError: 406 Not Acceptable - localhost:8084/crimeTrack/getCrimeTypeList.htm?crimeCatId=5""
-
OQJF about 11 yearsWhich version of your Spring? several release of Spring has this issue.
-
devdar about 11 years@QQJF i am using spring version 3.1
-
OQJF about 11 yearsOk, if you can please use the version 3.1.1 release, I helped another guy solve this issue yesterday whose version is Spring 3.2
-
devdar about 11 yearshey sorry i am using 3.1.1 release
-
devdar about 11 yearsi am going to update my code in the question i am getting a new error
-
Arun P Johny about 11 yearsdo you have jackson library in your classpath
-
OQJF about 11 yearsAnd did you add jackson.jar in your lib?
-
devdar about 11 yearsI added jackson.jar and it works thank you so much can you update your answer so i can accept it
-
OQJF about 11 yearshave updated. It's my pleasure that can give you my suggestion.
-
devdar about 11 yearsis there any way i can keep in contact with you dood?
-
OQJF about 11 yearsFacebook or Twitter: [email protected]