2014-02-28 32 views
0

請原諒我的問題是否已經與任何現有問題匹配。但是,我走過了很多stackoverflow相關解決方案的線程,但我無法找到我的問題的任何相關解決方案。請幫幫我。我很感謝他們。提前感謝。如何從ajax get()方法的spring控制器返回列表對象作爲json格式調用

實際上,我嘗試發送列表對象作爲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 請幫我在我犯錯的地方。預先感謝。並且我將jackson jar添加到我的classpath以及lib文件夾中。

+0

也許你不得不在classpath – gipinani

+0

@mserioli添加傑克遜罐子:我添加這些罐子,請看這裏http://d.pr/i/IM7T – vikky

回答

1

瀏覽器通常會發送Accept標頭,清楚地表明他們偏好HTML或XML響應。 Spring有一個默認註冊的XML轉換器,所以它會跳入並聲明它可以處理響應。但是vanilla List默認不能轉換爲XML(儘管它可轉換爲JSON)。您可以通過直接註冊HttpMessageConverters來解決問題,並確保如果有一個XML文件,則它前面是JSON文件。 (春季開機後此開箱偶然。)在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 
+0

我很抱歉,我無法找到你請問你可以用任何代碼片段來詳細說明這一點。 – vikky

+0

添加了Java配置示例。 –

+0

謝謝戴夫,我將把這段代碼應用到我的代碼中,我會盡快給你回覆。 – vikky

相關問題