简体   繁体   English

在SPRING中从Ajax调用响应时显示不可接受的错误

[英]Not Acceptable error showing while calling response from ajax in SPRING

I am trying to get response in form of json array from controller class. 我正在尝试从控制器类以json数组的形式获取响应。 When I tried to send request from javascript, I get "Not Acceptable" error. 当我尝试从javascript发送请求时,出现“不可接受”错误。 Please help to find b ug in my code. 请帮助在我的代码中找到错误。

This is my controller's code 这是我的控制器的代码

@RequestMapping(value = "/ajaxvalue", method = { RequestMethod.GET }, produces=MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody Map<String, String> geResourceList(String name){
        logger.info("getAllResources() from ajax begins: ");
        Map<String, String> resourceMap = null;
        try {
            logger.info("getAllResources() from ajax begin: ");
            resourceMap = new HashMap<String, String>();
            List<ResourceModel> resourceList = resourceService
                    .getAllResources();
            java.util.Iterator<ResourceModel> resourceIterator = resourceList.iterator();
            while (resourceIterator.hasNext()) {
                ResourceModel model = resourceIterator.next();
                resourceMap.put("" + model.getResourceType(),
                        model.getName());
                logger.info("" + model.getResourceType(),
                        model.getName());
            }

        } catch (Exception e) {
            logger.debug("Error while getting location in ajax request:"
                    + e.getMessage());
            e.printStackTrace();
        }logger.info("getAllResources() from ajax ENDS: ");
        return resourceMap;
    }

This is my jquery code. 这是我的jQuery代码。

jQuery.ajax({
url: '<c:url value='/learningresources/ajaxvalue'/>',
type: 'GET',
dataType:'json',
contentType: "application/json",
cache:false,
success:function(response){
    alert(response);
},
error:function(jqXhr, textStatus, errorThrown){
    alert("error: "+errorThrown);
}
});

try to add request header "Accept" to ajax call. 尝试将请求标头“ Accept”添加到ajax调用。

 headers: {
    'Accept':'application/json',
 }

This is most likely because of not having jackson library in class path, the following are two solutions depending on your configuration. 这很可能是因为类路径中没有杰克逊库,以下是两种解决方案,具体取决于您的配置。

if you are using maven then add the following dependency in pom.xml 如果您使用的是maven,则在pom.xml中添加以下依赖项

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>${jackson.version}</version>
</dependency>

If you are manually adding the jars then goto jackson download page and download version required according to your spring version. 如果您是手动添加罐子,请转到goto jackson下载页面,并根据您的春季版本下载所需的版本。

Make sure you get appropriate jackson version based on your spring version. 确保根据您的春季版本获得合适的杰克逊版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM