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

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状态码

SpringMVC在使用JSON时出现Error: Content type 'application/json;charset=UTF-8' not supported_第1张图片

错误详细信息:

主要信息: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"%>




Json交互








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中直接采用 自动装配Json转换器,如果没有开启注解驱动,
在注解适配器中加入messageConverters

 


	
		
		
		
		
		
	


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

解决方案

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

SpringMVC在使用JSON时出现Error: Content type 'application/json;charset=UTF-8' not supported_第2张图片

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

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

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(JavaEE,Spring,springmvc,json)