I’m experimenting with Scala in a spring3 project, and I’m not getting what I expect when I return a map with @ResponseBody. I’m including a working example in java along with my attempt in scala
// This is Java
@RequestMapping(value="/pbx.admin", method=RequestMethod.GET)
public @ResponseBody Map<String, Object> getInfo2(HttpServletRequest request, Model model){
Map<String, Object> map = new HashMap<String, Object>();
map.put("pbx", "admin");
map.put("method", "s");
return map;
}
The java returns json with the pbx & method defined, which is what I’m expecting.
// This is Scala
@RequestMapping(value= Array("/pbx.admin"),
method=Array(RequestMethod.GET))
@ResponseBody
def getInfo2() = {
Map("pbx" -> "admin", "method" -> "s")
}
The scala returns something different though:
{
empty: false,
traversableAgain: true
}
What do I need to do in order to get my map keys / values?
And for extra credit, Is there a better ‘scala way’ to do this? Thanks!
Spring MVC framework wasn’t built with Scala in mind and it doesn’t recognize Scala’s
Map. You must convertscala.collection.Maptojava.util.Map. You can either use implicit conversion:or convert it manually:
Mind the imports, they are important.
I suspect Spring can be hacked to accept Scala collections, but obviously not out-of-the box.