Java HttpMediaTypeNotAcceptableException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6386248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpMediaTypeNotAcceptableException
提问by Pinchy
I am having problem with my jQuery function what I am trying to achieve is to populate data in a listbox
我的 jQuery 函数有问题我想要实现的是在列表框中填充数据
The JavaScript function
JavaScript 函数
function load() {
$.getJSON('${findAdminGroupsURL}', {
ajax : 'true'
}, function(data) {
var html = '<option value="">Groups</option>';
var len = data.length;
for ( var i = 0; i < len; i++) {
html += '<option value="' + data[i].name + '">' + data[i].name
+ '</option>';
}
html += '</option>';
$('#selection').html(html);
});
}
The server side is
服务器端是
@RequestMapping(value = "groups", method = RequestMethod.GET)
public @ResponseBody
List<Group> getGroups() {
return this.businessGroups();
}
I call load() function on load it triggers the function getGroups() and returns the list successfully but the problem is once the getGroups() is finished
我在加载时调用 load() 函数,它触发函数 getGroups() 并成功返回列表,但问题是一旦 getGroups() 完成
function(data) doesn't load never gets into that function and the error is
函数(数据)不加载永远不会进入该函数,错误是
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示
Can't I post back a list of Group objects, or does it have to be a Java primitive type?
我不能回发一个 Group 对象列表,还是它必须是 Java 原始类型?
采纳答案by Boopathi Rajaa
A similar post I found today ..
我今天发现的一个类似的帖子..
Spring's Json not being resolved with appropriate response
Hope this might help
希望这可能会有所帮助
http://forum.springsource.org/showthread.php?85034-HttpMediaTypeNotAcceptableException-(always)
http://forum.springsource.org/showthread.php?85034-HttpMediaTypeNotAcceptableException-(总是)
回答by cherit
I am not sure which spring version you are using. I had the same problem and I solved it by adding the below Hymanson jars to my classpath.my spring version is 3.2.2
我不确定您使用的是哪个 spring 版本。我遇到了同样的问题,我通过将以下 Hymanson jar 添加到我的 classpath.my 中解决了它spring version is 3.2.2
Hymanson-mapper-asl
Hymanson-core-asl
Here is my controller
这是我的控制器
@RequestMapping(value="/{name}", method = RequestMethod.GET)
public @ResponseBody List<Supplier> getSuppliers(@PathVariable String name) {
searchDAO = (SearchDAO) SpringApplicationContext.getBean("searchDAO");
List<Supplier> suppliers = null;
try {
suppliers = searchDAO.searchSuppliersByZipCode(name);
//assertTrue(suppliers.size()>=1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return suppliers;
}
I only have mvc annotation in my application context , there is no need for explicit content negotiation. When you have @ResponseBody
, the default will json format and the Hymanson jars will be considered for conversion of your pojo.
我的应用程序上下文中只有 mvc 注释,不需要显式内容协商。当您有 时@ResponseBody
,默认将 json 格式并且 Hymanson jars 将被考虑用于转换您的 pojo。