`

Spring MVC 的json问题(406 Not Acceptable)

 
阅读更多

<mvc:annotation-driven />的缺失导致:


当请求:

 

    @RequestMapping("json1")
    public @ResponseBody UserType json1(Model model){
        return userTypeService.getById(6);
    }
 

将会提示:



 

 

 

然而当请求:

	@RequestMapping(value="json",method=RequestMethod.GET)
	public @ResponseBody String json(Model model){
		return "this is a string";
	}
 

 

 却是没有任何问题的:



 

对于这种现象,我也不知何原因!


Spring mvc 操作json的话,别忘记了  "  jackson-all-1.8.5.jar  "

 

 

  • 大小: 7.7 KB
  • 大小: 6 KB
分享到:
评论
7 楼 落雪封 2014-01-16  
谢谢,解决了,少导入jar包了
6 楼 duanbin87 2014-01-16  
wststar 写道
首先利用这种方式返回json数据,需要用到第三方的json包。
我选择jackson。需要导入jackson-mapper-asl-1.9.10.jar和jackson-core-asl-1.9.10.jar。
然后需要在Spring配置下:
<!-- 避免IE执行AJAX时,返回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>

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
			</list>
		</property>
	</bean>

wststar 写道
首先利用这种方式返回json数据,需要用到第三方的json包。
我选择jackson。需要导入jackson-mapper-asl-1.9.10.jar和jackson-core-asl-1.9.10.jar。
然后需要在Spring配置下:
<!-- 避免IE执行AJAX时,返回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>

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
			</list>
		</property>
	</bean>


亲测可以,thx
5 楼 落雪封 2014-01-10  
遇到了同样的问题,试过了,仍然不行
4 楼 xiaoyi148167576 2013-12-04  
非常感谢啊,前阵子就是出了这个错误,是因为springMVC3.0的jar不支持grid ajax方式请求后台 总是出现406错误

后来换成springMVC3.1的jar就直接OK了


现在又出现这个问题,发现不是jar的问题,找了很久,原来是少了<mvc:annotation-driven />的配置

多谢这位大神,我找了4个小时、、、、、、
3 楼 wststar 2013-01-18  
首先利用这种方式返回json数据,需要用到第三方的json包。
我选择jackson。需要导入jackson-mapper-asl-1.9.10.jar和jackson-core-asl-1.9.10.jar。
然后需要在Spring配置下:
<!-- 避免IE执行AJAX时,返回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>

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
			</list>
		</property>
	</bean>

2 楼 yangxixi 2012-12-05  
我也遇到了,但是按照你说的办法不行。我QQ是1509648627,相互交流下。。
1 楼 kizi1014 2012-05-11  
我也遇到这个问题,我返回一个bean也不行。

相关推荐

    Whether mandatory donation is acceptable or not?

    Whether mandatory donation is acceptable or not?

    jackson-core-asl-1.9.13及jackson-mapper-asl-1.9.13架包.rar

    可用于Spring MVC框架,spring MVC中返回使用@ResponseBody注解返回时,后台没报错,就控制台显示406 Not Acceptable 原因是缺少jackson的包:jackson-core-asl-1.9.2.jar和jackson-mapper-asl-1.9.2.jar

    elasticsearch-head

    ## Synopsis ... ## Motivation This was created because ElasticSearch 5 removed the ability to run ElasticSearch Head as an Elastic Plugin. This offers an alternative to self-hosting in your own web ...

    jackson的包:jackson-core-asl-1.9.2.jar和jackson-mapper-asl-1.9.2.jar

    spring MVC中返回使用@ResponseBody注解返回时,后台没报错,就控制台显示406 Not Acceptable 原因是缺少jackson的包:jackson-core-asl-1.9.2.jar和jackson-mapper-asl-1.9.2.jar

    Resizes Images to sizes acceptable by half life for transiti

    Resizes Images to sizes acceptable by half life for transitioning into pldecal.wad files This code is incomplete... A Majority of the programming has been accomplished.

    JSP页面报错列表查询

    406 Not Acceptable 指定的资源已经找到,但它的MIME类型和客户在Accpet头中所指定的不兼容(HTTP 1.1新)。 407 Proxy Authentication Required 类似于401,表示客户必须先经过代理服务器的授权。(HTTP 1.1新) ...

    ANSI-ASHRAE Standard 62.1-2019 Ventilation for Acceptable Indoor

    ANSI-ASHRAE Standard 62.1-2019 Ventilation for Acceptable Indoor

    json-api:与框架无关的JSON API(jsonapi.org)实现

    例如,它有助于正确地使用Unsupported Media Type (HTTP代码415)和Not Acceptable Unsupported Media Type (HTTP代码406)对无效请求做出响应。 您无需在每个请求上手动验证所有输入参数。 您可以配置服务支持...

    node-contrib-mvcgenerator:一种基于简单的JSON配置来搭建MVC项目的工具

    node-contrib-mvcgenerator一种基于简单的JSON配置来搭建MVC项目的工具。 注意:此项目无需维护-此处存在更好的替代方法: : 配置文件示例: exports . config = { // filename extensions - acceptable formats: js...

    Crawler.zip

    一个简单的网络爬虫资源包,是之前在发现的,文件包好像还挺完整,本来应该免积分的,但是因为急需积分,所以设置为两积分,有兴趣的可以下载试试

    play-content-negotiation:Play框架的声明式内容协商

    不幸的是,这种方法存在问题:如果服务器需要返回406 Not Acceptable HTTP响应,则Play框架无法在响应中提供受支持的内容类型的列表,这对于开发人员编程客户端非常有帮助。用于您的Web应用程序。 play-content-...

    rack-accepts:[UNMAINTAINED] 机架中间件拒绝蹩脚的 http 接受标头

    机架中间件拒绝蹩脚的 http 接受标头当请求不支持的类型时返回“406 Not Acceptable”状态。导轨用法 config.middleware.use Rack::Accepts执照版权所有 (c) 2010 Andrew Nesbitt 特此授予任何人免费获得本软件副本...

    SIP - Understanding the Session Initiation Protocol, 2nd Ed - 1459

    5.4.7 406 Not Acceptable 115 5.4.8 407 Proxy Authentication Required 115 5.4.9 408 Request Timeout 116 5.4.10 409 Conflict 116 5.4.11 410 Gone 116 5.4.12 411 Length Required 116 5.4.13 413 Request ...

    sip RFC3261 中文版

    21.4.7 406 Not Acceptable 209 21.4.8 407 Proxy Authentication Required 209 21.4.9 408 Request Timeout 210 21.4.10 410 Gone 210 21.4.11 413请求实体过大。 210 21.4.12 414 Request-URI Too Long 210 21.4....

    501 Command &quot;HELO&quot; requires an argument问题的解决方法

    换一个windows服务器,发现就没这样的问题,仅在一台Linux服务器上可以重现,直观感觉就是这台Linux服务器某些配置有问题

    How to implement SCP-ECG, Part II

    It is not anymore acceptable that, in the age of information technology, millions of Euros are wasted for unnecessarily repeated tests or that patients do not obtain appropriate care because relevant ...

    HowToImplement SCP-ECG_Part I

    It is not anymore acceptable that, in the age of information technology, millions of Euros are wasted for unnecessarily repeated tests or that patients do not obtain appropriate care because relevant ...

    LaTeX模板llncs

    部分国内外会议的LaTeX模板Lecture Notes in Computer Science (LNCS) ...Each contribution must be accompanied by a Springer copyright form, a so-called 'Consent to ... Modified forms are not acceptable.

    svm数据挖掘挖掘挖掘挖掘挖掘挖掘挖掘

    acceptable results. Although users do not need to understand the underlying theory behind SVM, we brie y introduce the basics necessary for explaining our procedure. A classi cation task usually ...

    node-crawler.zip

    msg: "Not Acceptable" } 就是 没有想要的值 现在就说对 x-sign 的的地方 通过node进行js的处理 进行数据的调用可以返回数为 返回结果为 ———————————————— 版权声明:本文为CSDN博主「我来了007...

Global site tag (gtag.js) - Google Analytics