繁体   English   中英

如何从ajax get()方法调用的spring控制器以json格式返回列表对象

[英]How to return the list object as a json format from spring controller of ajax get() method call

如果我的问题已经与任何现有问题都匹配,请原谅我。但是我遍历了众多与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文件夹中。

浏览器通常会发送“接受”标头,以明确表明他们更喜欢HTML或XML响应。 Spring默认情况下注册了一个XML转换器,因此它将跳入并声明其可以处理响应。 但是默认情况下,原始List无法转换为XML(尽管可以转换为JSON)。 您可以通过直接注册HttpMessageConverters并确保如果有一种XML并以JSON HttpMessageConverters方式来修复它。 (Spring Boot附带提供了开箱即用的功能。)在Java中,您可以这样做(只转换JSON,什么也没做):

@Configuration
public class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {

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

}

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

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

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

暂无
暂无

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

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