java 在 Spring 中使用 ResponseBody 注释返回 Json 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12449021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using ResponseBody annotation in Spring for returning a Json not working
提问by Adrian Bob
I am working on a spring mvc web application, in which I am using Google Visualization API for generating some charts. I have a model class which contains 2 arraylists, which represent the data that I`m sending to the function that draws the chart (this is what i want to be converted to a JSON).
我正在开发一个 spring mvc Web 应用程序,其中我使用 Google Visualization API 来生成一些图表。我有一个包含 2 个数组列表的模型类,它们表示我发送给绘制图表的函数的数据(这是我想要转换为 JSON 的内容)。
The model class:
模型类:
@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;
}
}
Then Ive got a
@Component` annotated class which contains a method that returns a model object (of the class that I wrote above), with the 2 arraylists attributes populated.
然后我ve got a
@Component` 注释的类包含一个方法,该方法返回一个模型对象(我上面写的类),填充了 2 个 arraylists 属性。
@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;}
}
So the Controller class, the one that has the mapping for the action that I am calling to gather data for the chart generation and in which I am calling listPieChartData
method, from the class above, and in which I'm also using the @ResponseBody
annotation is this:
因此,Controller 类具有我正在调用的操作的映射,以收集图表生成的数据,并在其中调用listPieChartData
方法,来自上面的类,并且我也在其中使用了@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 ); }
The JavaScript function that draws the chart :
绘制图表的 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);
}
}
And the AJAX call to the controller's mapped method (for gathering data) that finally calls the above JS function to obtain the chart (I'm also sending the request param int id for the controller method , I didn't wrote that)
以及对控制器映射方法(用于收集数据)的 AJAX 调用,最终调用上述 JS 函数来获取图表(我也发送了控制器方法的请求参数 int 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);
}
});
});
I know this is a lot of code :) but it's pretty simple code, to understand the flow better, here is how it`s working:
我知道这是很多代码 :) 但它是非常简单的代码,为了更好地理解流程,这里是它的工作原理:
From a button input I'm calling, with AJAX, the mapped method to the drawPieChart action (which is in the ChartController
class), this methods sends the response through invoking the listPieChart method (from the ChartUtils
class), which returns a JsonResponse
object, (which contains 2 arraylists). This JsonResponse
should be converted to a JSON, because in the AJAX request, I'm telling that the request needs a JSON input (via contentType: "application/json"), and it should get it because I use @ResponseBody
in the controller method mapped for this request.
从我使用 AJAX 调用的按钮输入到 drawPieChart 操作(在ChartController
类中)的映射方法,该方法通过调用 listPieChart 方法(从ChartUtils
类中)发送响应,该方法返回一个JsonResponse
对象,(包含 2 个数组列表)。这JsonResponse
应该转换为 JSON,因为在 AJAX 请求中,我告诉请求需要一个 JSON 输入(通过 contentType:“application/json”),它应该得到它,因为我@ResponseBody
在映射为的控制器方法中使用这个请求。
I`m getting this response:
我收到这个回复:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
(HTTP Status 406)
此请求标识的资源只能生成具有根据请求“接受”标头 () 不可接受的特征的响应。
(HTTP 状态 406)
Please correct me where I'm wrong, I just can't get this working and I can't figure out why...
请纠正我的错误,我就是无法正常工作,我不知道为什么......
And my servlet-context.xml
:
而我的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>
回答by Adrian Bob
So the problem was that i didn'd have all the Hymanson dependencies declared in pom.xml. These are the dependencies for your maven project in case you want Spring 3 to automatically serialize an object for you , using the @ResponseBody annotation , as a response from a method. Noob stuff , but I didn't saw this mentioned in the examples that I found .
所以问题是我没有在 pom.xml 中声明所有的 Hymanson 依赖项。这些是您的 Maven 项目的依赖项,以防您希望 Spring 3 使用 @ResponseBody 注释自动为您序列化一个对象,作为来自方法的响应。菜鸟的东西,但我没有看到我发现的例子中提到了这一点。
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-jaxrs</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-core-asl</artifactId>
<version>1.9.9</version>
</dependency>
Also , I had to change some stuff in the ajax call for invoking the method that is returning the json object with data for the chart generation :
此外,我不得不更改 ajax 调用中的一些内容,以调用返回带有图表生成数据的 json 对象的方法:
$("#buttonPieGenerate").live("click", function(){
$.ajax({
url: "drawPieChart", //method from controller
contentType: "application/json",
data: params,
success: function(data) {
drawPieChart(data.percentages, data.topics,'chart_div',600,400);
}
});
});
I'm accessing the data in the Json object that I`m getting as a response from the call with data.percentages , data.topics .
我正在访问 Json 对象中的数据,我从使用 data.percentages , data.topics 的调用中获得该数据作为响应。
回答by Michael Piefel
A small update for the world of 2015:
2015 年世界的一个小更新:
<dependency>
<!-- Just placing this on the classpath will enable JSON for @ResponseBody -->
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.5.3</version>
</dependency>