Warum @ResponseBody die sortierte LinkedHashMap als nicht sortiert zurückgibt?
Hier ist das SpringMVC-Controller-Code-Snippet:
@RequestMapping(value = "/getCityList", method = RequestMethod.POST)
public @ResponseBody LinkedHashMap<String, String> getCityList(@RequestParam(value = "countryCode") String countryCode, HttpServletRequest request) throws Exception {
//gets ordered city list of country [sorted by city name]
LinkedHashMap<String, String> cityList = uiOperationsService.getCityList(countryCode);
for (String s : cityList.values()) {
System.out.println(s); //prints sorted list [sorted by name]
}
return cityList;
}
Hier ist Ajax Anruf:
function fillCityList(countryCode) {
$.ajax({
type: "POST",
url: '/getCityList',
data: {countryCode:countryCode},
beforeSend:function(){
$('#city').html("<option value=''>-- SELECT --</option>" );
}
}).done(function (data) {
console.log(data); // UNSORTED JSON STRING [Actually sorted by key... not by city name]
})
}
Sorted LinkedHashMap gibt ein unsortiertes JSON-Objekt von der getCityList-Methode zurück. Warum wird die Bestellung während des Rückgabeprozesses geändert? Wird LinkedHashMap aufgrund der ResponseBody-Annotation in HashMap konvertiert? Ich kann mein sortiertes Objekt über die Gson-Bibliothek in eine Json-Zeichenfolge konvertieren und die Json-Zeichenfolge von meiner getCityList-Methode zurückgeben, aber diese Lösung gefällt mir nicht. Was kann ich tun, um eine JavaScript-Rückrufmethode mit sortierter Liste bereitzustellen?