SpringMVC在使用JSON时出现Error: Content type 'application/json;charset=UTF-8' not supported

5 篇文章 0 订阅
2 篇文章 0 订阅

SpringMVC在使用JSON时出现Error: Content type 'application/json;charset=UTF-8' not supported

      springmvc中使用jackson的包进行json转换(@requestBody和@responseBody使用下边的包进行json转),

问题阐述:

   今天在演示Springmvc实现json交互时,出现错误Content type 'application/json;charset=utf-8' not supported,一开始采用的Spring是4.2的版本,json的jar包是jackson-core-asl-1.9.11.jar和jackson-mapper-asl-1.9.11.jar,然后运行时就出错了,用火狐浏览器测试出现415状态码

错误详细信息:

主要信息:org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=utf-8' not supported,下面的就不贴了

代码展示

jsonTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Json交互</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
	//请求json,返回json
	function requestJson(){
		$.ajax({
			type:'post',
			url:'${pageContext.request.contextPath }/requestJson.action',
			contentType:'application/json;charset=utf-8',
			//数据格式是json串,商品信息
			data:'{"name":"手机","price":999}',
			success:function(data){//返回json结果
				alert(data);
			}
			
		});
	}
	//请求key/value,返回json
	function responseJson(){
		$.ajax({
			type:'post',
			url:'${pageContext.request.contextPath }/responseJson.action',
			//请求是key/value这里不需要指定contentType,因为默认就 是key/value类型
			//contentType:'application/json;charset=utf-8',
			//数据格式是json串,商品信息
			data:'name=手机&price=999',
			success:function(data){//返回json结果
				alert(data.name);
			}
		});
	}
	
</script>

</head>
<body>
<input type="button" οnclick="requestJson()" value="请求json,输出是json"/>
<input type="button" οnclick="responseJson()" value="请求key/value,输出是json"/>
</body>
</html>

JsonTest.java(Controller类)

package cn.gts.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gts.ssm.po.ItemsCustomer;

/**
 * Json交互的测试
 * @author gts
 *
 */
@Controller
public class JsonTest {
	
	/*
	 * 请求json,返回json
	 * @RequestBody:将请求的json数据转成ItemsCustomer对象
	 * @ResponseBody:将返回的ItemsCustomer对象转换成json串
	 */
	@RequestMapping("/requestJson")
	public @ResponseBody ItemsCustomer requestJson(@RequestBody ItemsCustomer itemsCustomer){
		System.out.println("00000");
		//@ResponseBody:将返回的ItemsCustomer对象转换成json串输出
		return itemsCustomer;
	}
	
	//请求key/value,输出json
		@RequestMapping("/responseJson")
		public @ResponseBody ItemsCustomer responseJson(ItemsCustomer itemsCustomer){
			
			//@ResponseBody将itemsCustom转成json输出
			return itemsCustomer;
		}
	
}


springmvc.xml中直接采用<mvc:annotation-driven /> 自动装配Json转换器,如果没有开启注解驱动,
在注解适配器中加入messageConverters

 

<!--注解适配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
		<list>
		<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
		</list>
		</property>
	</bean>


测试理念: 使用jquery的ajax提交json串,对输出的json结果进行解析。

解决方案

       需要将原来的json的jar升级,将原来的两个jar包换成现在的2.6.0版本的三个jar包,就好了,亲测可用!

 jackson2.6.0jar包下载http://download.csdn.net/detail/fpxty/9858089

 打开压缩包,里面有一个注意事项的TXT文件,可以注意下。

 

 

 

 

 

 

 

 

 

 

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值