我正在研究一個spring mvc web應用程序,其中我使用Google Visualization API生成一些圖表。我有一個模型類,其中包含2個數組列表,它們表示我發送給繪製圖表的函數的數據(這是我想要轉換爲JSON的數據)。在Spring中使用ResponseBody註釋來返回Json不工作
模型類:
@Component
public class JsonResponse {
private List<Integer> percentages = new ArrayList<Integer>();
private List<String> topics = new ArrayList<String>();
public JsonResponse(){
}
public List<Integer> getPercentages() {
return percentages;
}
public void setPercentages(List<Integer> percentages) {
this.percentages = percentages;
}
public List<String> getTopics() {
return topics;
}
public void setTopics(List<String> topics) {
this.topics = topics;
}
}
然後我ve got a
@ Component`註釋其包含返回一個模型對象(即我寫以上的類的)的方法,用2周的ArrayList類屬性填充。
@Component
public class ChartUtils {
@Autowired
public JsonResponse response;
public JsonResponse listPieChartData(ModelAndView model ,int waveId){
//arraylists for chart generation
List<Integer> percentages = new ArrayList<Integer>();
List<String> topics = new ArrayList<String>();
{... code for accessing the DB and processing some data and then populating the 2
arraylists ... }
response.setTopics(topics);
response.setPercentages(percentages);
return response;}
}
因此控制器類,即具有用於我打電話來收集數據的圖表生成,並且其中我打電話listPieChartData
方法的動作的映射,從類的上方,並且在其中予「M也使用@ResponseBody
註釋是這樣的:
@Controller
public class ChartController {
@Autowired
public ChartUtils utils;
@Autowired
public JsonResponse response;
@RequestMapping(value = "/drawPieChart", method = RequestMethod.GET)
@ResponseBody
public JsonResponse drawPieChart(ModelAndView model,
@RequestParam(value = "id", defaultValue = "-1") int waveId) {
return utils.listPieChartData(model,waveId); }
JavaScript函數繪製圖表:
function drawColumnChart(percentages, topics , div,width,height) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Wave');
for (var i=0; i < topics.length; i++){
data.addColumn('number', topics[i]);
}
data.addRow(percentages);
var wave=percentages[0];
var options = {
'title':'Generated Chart For '+wave,
'backgroundColor': { fill: "none" },
'is3D': true,
'width':width,
'height':height,
};
var chart = new google.visualization.ColumnChart(document.getElementById(div));
chart.draw(data, options);
}
而AJAX調用控制器的映射方法(用於收集數據),最終調用上述JS函數來獲取圖表(我也發送請求參數控制器方法的ID爲ID,我沒有寫那)
$("#button").live("click", function(){
var arrayP, arrayT;
$.ajax({
url: "drawPieChart",
contentType: "application/json",
data: params,
success: function(data) {
$.each(data, function(messageIndex, message) {
if (messageIndex === 0) {
arrayP = message;
} else {
arrayT = message;
}
});
drawPieChart(arrayP, arrayT,'chart_div',600,400);
}
});
});
我知道這是一個很大的代碼:)但它是非常簡單的代碼,瞭解流動好,這裏是如何工作的它了:
從按鈕輸入我用AJAX調用映射方法到drawPieChart動作(在ChartController
類中),該方法通過調用listPieChart來發送響應方法(來自ChartUtils
類),該方法返回一個JsonResponse
對象(其中包含2個數組列表)。這JsonResponse
應該轉換爲JSON,因爲在AJAX請求中,我告訴請求需要一個JSON輸入(通過contentType:「application/json」),它應該得到它,因爲我在控制器方法中使用@ResponseBody
映射此請求。
我真的收到這個響應:
由該請求所標識的資源僅能夠產生具有根據所述請求 不可接受特徵 響應的「接受」標題()。
(HTTP狀態406)
請糾正我在哪裏,我錯了,我只是不能得到這個工作,我想不通爲什麼...
而且我servlet-context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up
static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.bla.bla" />
<beans:import resource="classpath:springJDBC.xml" />
</beans:beans>
是否將產生= 「應用/ JSON」 你@RequestMapping幫助? – arahant
Hy,我試過了,我得到了同樣的結果,我也用螢火蟲來檢查請求,並且頭部包含Content-Type \t application/json,所以我真的不知道問題是什麼... –
重複http://stackoverflow.com/questions/3340050/springs-json-not-being-resolved-with-never-response? –