I was working with a web application using the Spring framework version 3.0.2 along with Hibernate (NetBeans 6.9.1). Later I came to know that there was one of bugs that was causing problems in uploading multiple files as mentioned in my one of previous questions.
I have finished struggling to acquire the solution but couldn't succeed. Therefore, I upgraded the Spring version to 3.2.0.
With the earlier version (3.0.2), AJAX was working fine with Jackson 1.9.8 (its download page) but with the later version (3.2.0), everything works fine but AJAX calls alert an error everywhere in the JavaScript code.
There is a scenario at one place when one of the countries is selected in the country select box, the corresponding state list is retrieved from the Spring controller along with DAO. The method which is mapped with a URL in the Spring controller is as follows,
@RequestMapping(value="ajax/GetStateList", method=RequestMethod.GET)
public @ResponseBody List<Object[]> getStateSelectBox(HttpServletRequest request)
{
return cityService.getStateSelectBox(request.getParameter("countryId"));
}
This method is invoked when a country is selected in the country select box. The getStateSelectBox()
method is defined in one of DAO classes as follows,
@Service
@Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW)
public final class CityDAO implements CityService
{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
public List<Object[]> getStateSelectBox(String id)
{
List<Object[]> list = sessionFactory.getCurrentSession()
.createQuery("select s.stateId, s.stateName from StateTable s where countryId.countryId=:id order by s.stateId")
.setParameter("id", Long.parseLong(id)).list();
for(Object[]o:list)
{
System.out.println(o[0]+" : "+o[1]);
}
return list;
}
}
The foreach
loop is just for the sake of demonstration, it displays all the states along with their id that correspond to the countryId
supplied by the AJAX request but the List
is not returned to JSP.
The JavaScript code used to send this AJAX request alerts an error. It appears that there are some problems with JSON mapping. The same thing was working with the earlier version of the Spring framework (3.0.2). I'm not sure why does this cause problems with the higher version of Spring which is 3.2.0. Is there anything with the Spring version 3.2.0 which I might be missing?
If you needed to see the JavaScript code, the full JavaScript code to achieve this would be as follows.
function getStates(countryId)
{
if(countryId==""||countryId==null||countryId==undefined||isNaN(countryId))
{
var str="<select id='cmbStates' name='cmbStates' onchange='errorMessage(this.value);' class='validate[required] text-input'><option value=''>Select</option></select>";
$('#stateList').html(str);
alert("Please select an appropriate option.");
return;
}
var div=document.createElement("div");
div.id="temp";
document.body.appendChild(div);
$.ajax({
datatype:"json",
type: "GET",
contentType: "application/json",
url: "/wagafashion/ajax/GetStateList.htm",
data: "countryId=" + countryId+"&t="+new Date().getTime(),
success: function(response)
{
if(typeof response==='object'&&response instanceof Array)
{
var str="<select id='cmbState' name='cmbState' onchange='errorMessage(this.value);' class='validate[required] text-input'><option value=''>Select</option>";
var l=response.length;
for(var i=0;i<l;i++)
{
str+="<option value='"+response[i][0]+"'>"+$('#temp').text(response[i][1]).html()+"</option>";
}
str+="</select>";
$('#stateList').html(str); // select box is written to #stateList div
$('#temp').remove();
}
},
error: function(e)
{
alert('Error: ' + e);
}
});
}
To be sure, the Jackson library is on the classpath and I'm not getting any error or exception on the server side. The AJAX request succeeds and it goes to the DAO via Spring and the list of type List<Object[]>
is retrieved from the database but it is not a response of JSON to JSP (which could/should be mapped to a JavaScript array). presumably, it appears that there is something missing with JSON mapping which was however not the case with the earlier version of Spring.
EDIT:
I have tried to parse List<Object[]>
in both of the frameworks, 3.0.2 and 3.2.0 such as
List<Object[]> list = cityService.getStateSelectBox(request.getParameter("countryId"));
ObjectMapper objectMapper=new ObjectMapper();
try
{
objectMapper.writeValue(new File("E:/Project/SpringHibernet/wagafashionLatest/temp.json"), list);
}
catch (IOException ex){}
The file temp.json
contains the following string.
[[21,"Gujarat"],[22,"Maharashtra"],[23,"Kerala"],[24,"New Delhi"]]
In both the cases (with both frameworks). So, it appears that the JSON response should be same in both the cases.
The temp.json
file can also be deserialized as follows.
try
{
ObjectMapper mapper=new ObjectMapper();
List<Object[]> list = mapper.readValue(new File("E:/Project/SpringHibernet/wagafashionLatest/temp.json"), new TypeReference<List<Object[]>>() {});
for(Object[]o:list)
{
System.out.println(o[0]+" : "+o[1]);
}
}
catch (IOException ex)
{
}
It works fine and the foreach
loop iterates over the List
of type List<Object[]>
. So, the problem might be caused by the Spring framework itself. What else is required, I'm not sure. Why is it not mapped by Jackson?
I'm using Spring 3.1.3 and I've found that the Jackson mapping attempts to create a root object in the response. This is with the Jackson2 mapper. I haven't tried with the older Jackson mapper. If you also upgraded Jackson you may be seeing the same issue.
In the past, an object array would be mapped something like
Now I find that they are providing an auto-generated object name for the object, so it is returned something like
So you need to reference
response.objectArray[0]
instead of being able to directly referenceresponse[0]
In any case, it's probable that the JSON response has changed format somewhat. You should look at the new response and see what changes need to occur in the javascript to make it map the new structure.
I have the same post on the Spring forum. The solution from the replies to the question which ultimately worked for me was required to configure the
dispatcher-servlet.xml
file as follows.This led JSON to work with Jackson 1.9.8 and Spring 3.2.0. This is credited to all the replies to that question.
My entire
dispatcher-servlet.xml
file now looks like the following.