自定義HTTPMessageConverter接收JSON格式的資料
阿新 • • 發佈:2018-12-31
jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'json2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
testjson2();
});
function testjson2(){
$.ajax({
url:"${pageContext.request.contextPath}/json/testRequestBody.htm",
dataType:"json",//預期伺服器返回的型別
type:"post",
contentType:"application/json",//傳送資訊到伺服器時編碼格式
//傳送到伺服器的資料
data:JSON.stringify({id:1,name:"SpringMVC 企業應用實戰"}),
async:true,//表示非同步
success:function(data){
$("#id").html(data.id);
$("#name").html(data.name);
$("#auther").html(data.auther);
},
error:function(){
alert("資料傳送失敗");
}
});
}
</script>
</head>
<body>
編號:<span id="id"></span><br>
書名:<span id="name"></span><br>
作者:<span id="auther"></span><br>
</body>
</html>
controller類
package com.anbow.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.anbow.bean.Book;
@Controller
@RequestMapping(value=”/json”)
public class BookController {
@RequestMapping(value="/testRequestBody")
public void setJson(@RequestBody Book book,HttpServletResponse response){
//JsonObject-lib是一個beans,controllers,maps,Java arrays 和xml和json互相轉換的包
book.setAuther("喬峰");
response.setContentType("text/html;charset=UTF-8");
try {
response.getWriter().print(JSONObject.toJSONString(book));
} catch (IOException e) {
e.printStackTrace();
}
}
}
servlet-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" default-autowire="byName">
<!-- spring可以自動掃描有base package下面的包或者是子包下面的Java檔案
如果掃描到有spring的相關注解的類,則把這些類註冊為spring的bean
-->
<context:component-scan base-package="com.anbow"></context:component-scan>
<!-- 使用預設的servlet來響應靜態檔案 -->
<mvc:default-servlet-handler/>
<!-- 設定配置方案 -->
<mvc:annotation-driven>
<!-- 設定不使用預設的訊息轉換器 -->
<mvc:message-converters register-defaults="false">
<!-- 配置spring的轉換器 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"></bean>
<!-- 配置fastjson 中實現HttpMessageConverter介面的轉換器 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 加入支援的媒體型別 -->
<property name="supportedMediaTypes">
<list>
<!-- 這裡順序不能反,一定先寫text/html,不然IE下會出現下載提示 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 檢視解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>