用springmvc的注解@responsebody 无法返回json数据 不知道why

dafengasp 2014-11-26 11:35:17
刚开始用sringmvc 在方法上用注解@responsebody 无法返回json数据 为啥 呢
...全文
17701 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
vip3652204732 2018-06-06
  • 打赏
  • 举报
回复
引用 18 楼 abc20072349 的回复:
[quote=引用 6 楼 dafengasp 的回复:] [quote=引用 5 楼 whos2002110 的回复:] 你没有提供序列化的包吧. org.codehaus.jackson 和 com.fasterxml.jackson.core 至少提供一个. 建议使用后者
提供包了 官网拷贝的 写程序没报错 要是没包 程序会报错 的啊 就是没法 返回json数据[/quote] 找了半天原因,原来是这个啊,太感谢了。[/quote] 我的pom 文件里面配置了这个 <!--ajax jackson-core jar --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <!--ajax jackson-mapping jar --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
vip3652204732 2018-06-06
  • 打赏
  • 举报
回复
引用 3 楼 a331251021 的回复:
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="stringHttpMessageConverter" />
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
少了一个
我的Spring是5.0.6 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 这个不存在。
Fibonching 2015-08-16
  • 打赏
  • 举报
回复
我也是 ~~这特么几个意思 火狐就是可以显示jaon的数据。
江小鱼NO1 2015-07-01
  • 打赏
  • 举报
回复
引用 6 楼 dafengasp 的回复:
[quote=引用 5 楼 whos2002110 的回复:] 你没有提供序列化的包吧. org.codehaus.jackson 和 com.fasterxml.jackson.core 至少提供一个. 建议使用后者
提供包了 官网拷贝的 写程序没报错 要是没包 程序会报错 的啊 就是没法 返回json数据[/quote] 找了半天原因,原来是这个啊,太感谢了。
kky2010_110 2014-11-27
  • 打赏
  • 举报
回复
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean>
dafengasp 2014-11-27
  • 打赏
  • 举报
回复
引用 3 楼 a331251021 的回复:
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="stringHttpMessageConverter" />
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
少了一个
邪门啊 吧你的代码拷贝进去IE浏览器打开还是弹出下载啊 无法返回JSON数据 <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> </list> </property> </bean> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean>
dafengasp 2014-11-26
  • 打赏
  • 举报
回复
引用 1 楼 rui888 的回复:
你最后是json 格式的数据吗?
不是就是在方法上加@responsebody
a331251021 2014-11-26
  • 打赏
  • 举报
回复
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="stringHttpMessageConverter" />
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
少了一个
a331251021 2014-11-26
  • 打赏
  • 举报
回复
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
	<property name="supportedMediaTypes">
	<list>
	<value>text/html;charset=UTF-8</value>
	</list>
	</property>
	</bean>

	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="stringHttpMessageConverter" />
			</list>
		</property>
	</bean>

	<bean id="stringHttpMessageConverter"
		class="org.springframework.http.converter.StringHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>
配置这东西
tony4geek 2014-11-26
  • 打赏
  • 举报
回复
你最后是json 格式的数据吗?
dafengasp 2014-11-26
  • 打赏
  • 举报
回复
引用 13 楼 rui888 的回复:
参考这个呢。
草 看了某某堂的视频 拿它源码过来还是在IE弹出下载页面 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="cacheSeconds" value="0" /> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property> </bean>
dafengasp 2014-11-26
  • 打赏
  • 举报
回复
引用 13 楼 rui888 的回复:
参考这个呢。
没用 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> </bean> </list> </property> </bean > 我这样配置的 在火狐能返回JSON 但是IE就是 下载
tony4geek 2014-11-26
  • 打赏
  • 举报
回复
tony4geek 2014-11-26
  • 打赏
  • 举报
回复
你是没有配置jsonConverter
dafengasp 2014-11-26
  • 打赏
  • 举报
回复
dafengasp 2014-11-26
  • 打赏
  • 举报
回复
引用 8 楼 rui888 的回复:
你现在 debug 后返回格式是什么样子的。
刚才是没有添加jackson-core-asl-1.9.13+jackson-mapper-asl-1.9.13 添加完了 不报错了 返回让我下载
dafengasp 2014-11-26
  • 打赏
  • 举报
回复
引用 8 楼 rui888 的回复:
你现在 debug 后返回格式是什么样子的。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 中央控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- struts习惯使用/*,在springmvc不管用 --> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- mvc的注解驱动 --> <mvc:annotation-driven/> <!-- 一旦有扫描器的定义mvc:annotation-driven不需要,扫描器已经有了注解驱动的功能 --> <context:component-scan base-package="com.asp"/> <!-- 前缀+ viewName +后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- webroot到某一指定的文件夹的路径 --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- 视图名称的后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans> @Controller public class Test { @RequestMapping("/hello.do") public @ResponseBody List<Person> hello(){ ArrayList<Person> list=new ArrayList<Person>(); Person p1=new Person(); p1.setAge(10); p1.setCity("上海"); p1.setName("小王"); Person p2=new Person(); p2.setAge(20); p2.setCity("北京"); p2.setName("小张"); list.add(p1); list.add(p2); return list; } } 我在浏览器输 http://localhost:8080/asptest/hello.do 返回的错误 HTTP Status 406 - -------------------------------------------------------------------------------- type Status report message description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
tony4geek 2014-11-26
  • 打赏
  • 举报
回复
你现在 debug 后返回格式是什么样子的。
MIT的梦 2014-11-26
  • 打赏
  • 举报
回复
2楼正解,你没配置,没有包,不是简单写个@responsebody就行的
dafengasp 2014-11-26
  • 打赏
  • 举报
回复
引用 5 楼 whos2002110 的回复:
你没有提供序列化的包吧. org.codehaus.jackson 和 com.fasterxml.jackson.core 至少提供一个. 建议使用后者
提供包了 官网拷贝的 写程序没报错 要是没包 程序会报错 的啊 就是没法 返回json数据
加载更多回复(1)

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧