从spring mvc controller返回一个简单的map结构到ajax [英] Returning a simple map structure from spring mvc controller to ajax
问题描述
我正在使用spring mvc 4并尝试将一个简单的 map
返回给ajax - 从我的控制器到jsp文件。
I am using spring mvc 4 and trying to return a simple map
to ajax - from my controller to jsp file.
控制器:
@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;
}
来自jsp文件的Ajax:
Ajax from the jsp file:
function testAjax() {
$.ajax({
url : '../../ajaxtest.html',
dataType: "json",
contentType: "application/json;charset=utf-8",
success : function(data) {
alert("1");
alert(data);
}
});
}
但是我没有得到任何警报,只是错误 HTTP / 1.1 406不可接受
。
But I am not getting any alert but just the error HTTP/1.1 406 Not Acceptable
.
但是,更改代码以返回简单字符串工作正常。
控制器:
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);
}
});
}
提醒 1
和按照预期从ajax hello
。
Alerting 1
and hello
from ajax as expected.
我按预期添加了jackson jar文件(通过pom.xml):
I added the jackson jar 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 other class structure in the future).
更新:
从spring控制台(不确定它是否相关):
解析处理程序的异常[null]:org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示
提前致谢!
Mike
Thanks in advance! Mike
推荐答案
我不知道它是否正确,但我解决了以下问题。
I don't know if it the correct way but I solved it as following.
在控制器中,我将地图转换为json:
In the controller, I converted the map to json:
@RequestMapping(value = "/ajaxtest", method = RequestMethod.GET)
public @ResponseBody
String myTest() {
System.out.println("------------------------------------random");
Map<String,String> myMap = new HashMap<String, String>();
myMap.put("a", "1");
myMap.put("b", "2");
ObjectMapper mapper = new ObjectMapper();
String json = "";
try {
json = mapper.writeValueAsString(myMap);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
和jsp:
function testAjax() {
$.ajax({
url : '../../ajaxtest.html',
type:"GET",
contentType: "application/json;charset=utf-8",
success : function(data) {
alert("1");
alert(data);
var obj = jQuery.parseJSON( data );
alert(obj.a);
alert(obj.b);
}
});
谢谢大家!
Mike
}
Thanks you all! Mike }
这篇关于从spring mvc controller返回一个简单的map结构到ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!