1. 程式人生 > >springmvc返回json格式資料報406錯誤

springmvc返回json格式資料報406錯誤

一、配置springmvc

1. springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="
			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
			http://www.springframework.org/schema/tx
			http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
			http://www.springframework.org/schema/util
			http://www.springframework.org/schema/util/spring-util-3.0.xsd 
			http://www.springframework.org/schema/cache 
			http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
	
	<context:component-scan base-package="com"/>

</beans>
2. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name></display-name>

	<!-- DispatcherServlet -->
	<servlet>
		<servlet-name>service</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:/springmvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>service</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

3. jsp與action
<input type="button" id="btn" value="查詢">

$("#btn").click(function() {
	$.ajax({
		url : '${basepath}/index/test.do',
		dataType : "json",
		type : "POST",
		async : true,
		data : {
			"id" : "wangfei"
		},
		success : function(data) {
			alert(data.msg);
		},
		error : function() {
			alert("error");
		}
	});
})
@Controller
@RequestMapping("index")
public class Index {

	 @RequestMapping("test")
	 @ResponseBody 
	 public JSONObject getAll(HttpServletRequest request){
		 JSONObject json = new JSONObject();
		 String id = request.getParameter("id");
		 json.put("msg", "json:"+id);
		 return json;
	 }
}
報錯提示


二、解決返回json資料錯誤

在springmvc-servlet.xml配置檔案中加如下程式碼

<!--JSON檢視,需要jacksonJSON的兩個jar-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
	<property name="messageConverters">
		<util:list id="beanList">
			<ref bean="mappingJacksonHttpMessageConverter" />
		</util:list>
	</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
	<property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

返回結果:


注:相應的jar包沒有放上去,主要有:spring相關,json相關,如有遺漏,根據報錯,自行添補。

相關推薦

springmvc返回json格式資料406錯誤

一、配置springmvc 1. springmvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schem

配置ajax請求springmvc返回json格式資料

首先在controller的方法返回值(返回值型別為實體類)上增加@ResponseBody註解 然後兩種配置方式如下: 方式一:spring4.0以及之後版本推薦 pom.xml增加如下依賴包 &l

Spring 3.2.* MVC通過Ajax獲取JSON資料406錯誤

Spring 3.2.x通過@ResponseBody標籤返回JSON資料的方法都報406錯: Failed to load resource: the server responded with a status of 406 (Not Acceptable) 以及報錯描述: The resource i

SpringMVC 統一返回JSON格式資料到前端

有時在給APP做介面功能的時候,都是返回JSON格式的資料,所以最好的只好在工程設定一個統一的資料返回方式        在SpringMVC 直接配置XML可以產生這種配置,比較簡單        S

Springmvc返回JSON格式到頁面中文亂碼問題

spa com prope edm rop prop tps 編碼 我們 我是通過@ResponseBody註解的方式實現json格式傳到頁面的方法。首先查看源代碼如下圖,springmvc的默認編碼是“ISO-8859-1”; 而我們通常編碼都是

頁面訪問伺服器返回json格式資料太大,導致資料不全被截斷,無法展示

問題:頁面展示呼叫查詢方法查詢全部資料的時候一直顯示loading。。。,開啟偵錯程式顯示 Failed to load resource: net::ERR_SPDY_PROTOCOL_ERROR,而少部分查詢則正常顯示。 因為資料中有圖片轉成的二進位制陣列,資料比較長,由此懷疑

Struts2返回json格式資料踩坑記錄

事件起因 昨天提測修改凍結/解凍銀行卡樣式的功能,微姐測試過程中發現呼叫ajax請求耗時過長,今天來排查,發現瀏覽器請求/finance/ajax/freeze/ajaxGetShopLists時,對應的後端方法getShopList()莫名其妙地執行了兩遍,並且返回給瀏覽器的Json字串如下:

SpringMVC返回json格式

新增 @ResponseBody 即可 /** * 根據裝置號、開始時間、結束時間獲取客戶行駛軌跡 * @param gpsId * @param startTime * @param endTime * @return

ajax返回json格式資料

控制器中這樣寫 //刪除二維碼 public function shanchu() { $data = $_POST['a']; $shanchu = K::M('code/content')->shanchu($data); $returnData =

Python flask中mysql查詢返回json格式資料

在進行web專案開發時,經常需要查詢資料庫並返回json格式給前端,Java中可以使用JSONObject或mybatis直接就可以對映成map結構,而python中返回的是元組的形式(('a',1,2), ('b',2,3), ...)。下面提供幾種在pyth

SpringBoot+Jpa 在MySql中自動生成時間和返回json格式資料時間格式配置

先說資料時間格式的設定 有兩種 一種是:可以在apllication.property加入下面配置就可以 #時間戳統一轉換 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=

解決後臺返回JSON格式資料,IE瀏覽器提示下載的問題

解決後臺返回JSON格式資料,IE瀏覽器提示下載的問題 問題描述 在近期的一個專案中,使用前後端分離開發,後端使用SpringMVC向前臺返回JSON資料的時候,其他瀏覽器都可以正常顯示,唯獨IE瀏覽器讓人蛋疼不已,每次都要下載下來才能開啟,真的很讓人抓狂,於是老套路上網一頓查資料,

@ResponseBody返回Json格式資料問題

@ResponseBody 作用:       該註解用於將Controller的方法返回的物件,通過適當的HttpMessageConverter轉換為指定格式後,寫入到Response物件的body資料區。 使用時機:       返回的資料不是html標籤的頁面

springboot 返回json格式資料時間格式配置

mybatis資料查詢返回的時間,是一串數字,如何轉化成時間。兩種方法,推薦第一種方法一:可以在apllication.property加入下面配置就可以#時間戳統一轉換spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.

Flask設定返回json格式資料

問題描述 在Flask中直接返回list或dict是不行的,如 from flask import Flask app = Flask(__name__) @app.route('/') def root(): t = {

使用jQuery傳送POST,Ajax請求返回JSON格式資料

問題:使用jQuery POST提交資料到PHP檔案, PHP返回的json_encode後的陣列資料,但jQuery接收到的資料不能解析為JSON物件,而是字串{"code":-1,"msg":"12

springmvc接收json格式資料

controller程式碼如下:@RequestMapping(value="/userInfo",method=RequestMethod.POST)@ResponseBodypublic String getUser(@RequestBody User user){   

django 返回json格式資料

下面簡單介紹如何使用httpresponse 自定義結果,並使用json格式,返回給客戶端。 import json from django.http import HttpResponse de

SpringBoot 提交Json格式資料錯及解決方法

錯誤資訊 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' no

Spring mvc 使用 jackson2 返回json格式資料資料的構成

xml: <bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <pro