I am using spring mvc 4 and trying to return a simple map in ajax - from my controller to a jsp file.
Controller:
@RequestMapping(value = "/ajaxtest", method = RequestMethod.GET) public @ResponseBody Map<String, String> myTest() { System.out.println("------------------------------------test"); Map<String,String> myMap = new HashMap<String, String>(); myMap.put("a", "1"); myMap.put("b", "2"); return myMap; }
Ajax from jsp file:
function testAjax() { $.ajax({ url : '../../ajaxtest.html', dataType: "json", contentType: "application/json;charset=utf-8", success : function(data) { alert("1"); alert(data); } }); }
But I am not getting a warning, but just an HTTP/1.1 406 Not Acceptable error.
However, changing the code to return a simple string works fine. Controller:
@RequestMapping(value = "/ajaxtest", method = RequestMethod.GET) public @ResponseBody String myTest() { System.out.println("------------------------------------test"); return "hello"; }
Ajax:
function testAjax() { $.ajax({ url : '../../ajaxtest.html', success : function(data) { alert("1"); alert(data); } }); }
Warning 1 and hello from ajax as expected.
I added json json files as expected (by pom.xml):
dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.1</version> </dependency>
Am I missing something? I just want to return a simple mapping structure (or another class structure in the future).
Update : From the spring console (not sure if this is related): Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
Thanks in advance! Mike
java json jackson ajax spring-mvc
Mike
source share