【问题标题】:How to return the list object as a json format from spring controller of ajax get() method call如何从 ajax get() 方法调用的 spring 控制器将列表对象作为 json 格式返回
【发布时间】:2014-02-28 13:19:46
【问题描述】:

如果我的问题已经与任何现有问题匹配,请原谅我。但是我浏览了很多与 stackoverflow 相关的解决方案的线程,但我无法找到我的问题的任何相关解决方案。请帮我。我很感谢他们。提前致谢。

实际上,我尝试以 json 格式发送列表对象作为 ajax 调用的响应。

这是我为实现此功能而编写的代码,

My Controller

 package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.daos.StudentDao;
import com.user.model.Student;

@Controller
public class Registration {

    @RequestMapping(value = "/registration", method = RequestMethod.GET)

    public @ResponseBody List studentReg() {

        Map<Integer, String> map1 = new HashMap();
        map1.put(1, "a");
        map1.put(2, "b");
        Map<Integer, String> map2 = new HashMap();
        map2.put(3, "c");
        map2.put(4, "d");
        Map<Integer, String> map3 = new HashMap();
        map3.put(5, "e");
        map3.put(6, "f");
        List<Map> list = new ArrayList();
        list.add(map1);
        list.add(map2);
        list.add(map3);

        return list;
    }
}

My spring cinfiguration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
    <mvc:annotation-driven />
    <context:component-scan base-package="controllers" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

And my Ajax call is

$(document).ready(function($) {

    $("#abc").click(function(event) {
        event.preventDefault();
        $.ajax( {
            type : "Get",
            url : "registration",
            success : function(response) {
                alert(response);
            },
            error : function(e) {
                alert('Error: ' + e);
            }
        });
    });

});

最后我在渲染响应http://d.pr/i/lnzL时遇到了这个异常 请帮助我在哪里做错了。在此先感谢。我将杰克逊罐子添加到我的类路径和 lib 文件夹中。

【问题讨论】:

  • 也许你必须在你的类路径中添加jackson jars
  • @mserioli : 我添加了那些罐子,请看这里d.pr/i/IM7T

标签: java javascript ajax json spring


【解决方案1】:

浏览器通常会发送 Accept 标头,清楚地表明他们更喜欢 HTML 或 XML 响应。 Spring 默认注册了一个 XML 转换器,因此它会跳进去并声称它可以处理响应。但是默认情况下,vanilla List 不能转换为 XML(尽管它可以转换为 JSON)。您可以通过直接注册HttpMessageConverters 来修复它,并确保如果有 XML 则它前面是 JSON 。 (Spring Boot 是开箱即用的。)在 Java 中,你可以这样做(只转换 JSON,而不是其他):

@Configuration
public class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.addAll(new MappingJackson2HttpMessageConverter());
}

}

我认为在 XML 中有一个用于消息转换器的命名空间元素(您应该能够使用 XML 编辑器找到它)。

您的类路径中还需要 Jackson(每个 cmets)。您可以通过不使用浏览器加载资源来测试它是否存在(例如,在命令行上使用 curl 或许多其他工具之一,例如允许您控制标头的浏览器插件)。例如

$ curl -H "Accept: application/json" myhost/registration

【讨论】:

  • 很抱歉,我无法联系到您。请您使用任何代码 sn-ps 详细说明一下。
  • 添加了 Java 配置示例。
  • 谢谢 Dave,我会将这个 sn-p 应用到我的代码中,我会以最终结果回复您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多