springmvc-自定义消息转换器
最近的项目没有用到这个,先把自己自学跑通的例子先帖出来,供自己以后参考吧!
如有不对地方望指出!
一、自定义类实现AbstractHttpMessageConverter
package com.dzf.converter; import java.io.IOException;
import java.nio.charset.Charset; import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StreamUtils; import com.alibaba.fastjson.JSONObject;
import com.dzf.vo.ResultInfo;
/**
* 自定义消息转换器
* @author dingzf
* @date 2018年1月20日
* @time 19:26:39
*/
public class MyMessageConverter extends AbstractHttpMessageConverter<ResultInfo> { public MyMessageConverter(){
super(Charset.forName("utf-8"),new MediaType("application","x-result"));//application/x-result 自己定义的媒体数据类型
} //所映射的model
@Override
protected boolean supports(Class<?> clazz) {
return ResultInfo.class.isAssignableFrom(clazz);
} @Override
protected ResultInfo readInternal(Class<? extends ResultInfo> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
String copyToString = StreamUtils.copyToString(inputMessage.getBody(), Charset.forName("utf-8"));//传输为json类型的数据
ResultInfo result = (ResultInfo)JSONObject.parse(copyToString);//转换为自己想要的数据类型 按需编写
return result;
} @Override
protected void writeInternal(ResultInfo t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
String str = t.getCode()+"-"+t.getDesc();//返回到前台的数据
outputMessage.getBody().write(str.getBytes());
} }
二、在springmvc的配置文件中加入我们自定义的消息转换器
<!--
打开springmvc的注解模式
mvc 请求映射 处理器与适配器配置 -->
<mvc:annotation-driven >
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter" ><!--字符串转换器-->
<property name = "supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /><!--json转换器-->
<bean class ="com.dzf.converter.MyMessageConverter"> <!--自己定义的消息转换器-->
<property name = "supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
<value>application/x-result;charset=utf-8</value>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
三、在前台指定发送数据的格式
function test6(){
$.ajax({
type:'post',
url:'json/test6',
contentType:'application/x-result',
data:{code:"200",desc:"我是丁振锋"},
success:function(text){
alert(text);
},
error:function(data){
alert("后台异常!")
},
asyn:false,
cache:false
});
}
四、服务器端指定返回的数据格式
/**
* 测试自定义消息转换器
* 在请求头上必须加上produces="application/x-result;charset=utf-8"
* @param request
* @param response
* @return
*/
@RequestMapping(value="/test6",produces="application/x-result;charset=utf-8")
@ResponseBody
public ResultInfo test6(HttpServletRequest request,HttpServletResponse response){
ResultInfo result = new ResultInfo();
result.setCode("200");
result.setDesc("请求成功!");
Map<String,String> map = new HashMap<String,String>();
map.put("name", "红霞");
map.put("age","22");
result.setData(map);
return result;
}
到这里,基本上就结束了!
注意点:
1.请求头的contentType必须要设值你自定义的数据格式
2.返回数据格式如果需要使用你自定义的数据格式,加上路由设置。即:produces="application/x-result;charset=utf-8"
springmvc-自定义消息转换器的更多相关文章
- springmvc 类型转换器 自定义类型转换器
自定义类型转换器的步骤: 1.定义类型转换器 2.类型转换器的注册(在springmvc配置文件处理) 来解决多种日期格式的问题: springmvc 类型转换器 表单数据填错后返回表单页面(接上面的 ...
- JavaEE开发之SpringMVC中的自定义消息转换器与文件上传
上篇博客我们详细的聊了<JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术>,本篇博客依然是JavaEE开发中的内容,我们就来聊一下SpringMVC中的自定义消息转发器 ...
- SpringMVC类型转换器、属性编辑器
对于MVC框架,参数绑定一直觉得是很神奇很方便的一个东西,在参数绑定的过程中利用了属性编辑器.类型转换器 参数绑定流程 参数绑定:把请求中的数据,转化成指定类型的对象,交给处理请求的方法 请求进入到D ...
- SpringMVC——消息转换器HttpMessageConverter(转)
文章转自http://blog.csdn.net/cq1982/article/details/44101293 概述 在SpringMVC中,可以使用@RequestBody和@ResponseBo ...
- SpringBoot添加自定义消息转换器
首先我们需要明白一个概念:springboot中很多配置都是使用了条件注解进行判断一个配置或者引入的类是否在容器中存在,如果存在会如何,如果不存在会如何. 也就是说,有些配置会在springboot中 ...
- springboot自定义消息转换器HttpMessageConverter
在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,底层这种灵活的消息转换机制就是利用HttpMessageCo ...
- springmvc 类型转换器 数据回显及提示信息
处理器的写法: 类型转换器的写法: 类型转换器在springmvc.xml中的配置如下: index.jsp的写法:
- springmvc 日期转换器
package com.xxx.common.controller.converter; import org.joda.time.DateTime; import org.joda.time.for ...
- Spring MVC自定义消息转换器(可解决Long类型数据传入前端精度丢失的问题)
1.前言 对于Long 类型的数据,如果我们在Controller层通过@ResponseBody将返回数据自动转换成json时,不做任何处理,而直接传给前端的话,在Long长度大于17位时会出现精度 ...
- springboot自定义消息转换器HttpMessageConverter Spring Boot - 使用Gson替换Jackson
Jackson一直是springframework默认的json库,从4.1开始,springframework支持通过配置GsonHttpMessageConverter的方式使用Gson. 在典型 ...
随机推荐
- Ancient Go---hdu5546(dfs爆搜CCPC题目)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 题意就是两个人下围棋,问在下一颗x是否能杀死o,'.'是空位子: 枚举所有的点,判断是否合法即可 ...
- 如何下载Bilibili视频
方法1: https://www.bilibili.com/video/av25940642 (源网址) https://www.ibilibili.com/video/av25940642 (新网址 ...
- 如何用 Keynote 制作动画演示(转)
原文:如何用 Keynote 制作动画演示 Keynote 里的很多特效可以用来制作效果不错的演示,一页页的将需要演示的内容交代清楚后,直接输出成 m4v 的视频格式,为了方便贴到博客或者发布到 Tw ...
- 【Python】唯品会购买商品
操作过程:唯品会进入之后,搜索商品,浏览网页,略掉不能选择的尺寸,选择之后,点击商品选择数量的加号,然后加入购物车. 实现代码如下: # coding=utf-8 from selenium impo ...
- 【Python虫师】多窗口定位
<注意>iframe框架 iframe也称作嵌入式框架,嵌入式框架和框架网页类似,它可以把一个网页的框架和内容嵌入在现有的网页中. 框架(framework)是一个基本概念上的结构,用于去 ...
- python 基础 字典
字典操作 字典一种key - value 的数据类型 特性: 无顺序 去重 查询速度快,比列表快多了 比list占用内存多 语法: info = { 'abc001': "Ben" ...
- Java-idea-eclipse-快捷键【mac,win】
Mac键盘符号和修饰键说明 ⌘ Command ⇧ Shift ⌥ Option ⌃ Control ↩︎ Return/Enter ⌫ Delete ⌦ 向前删除键(Fn+Delete) ↑ 上箭头 ...
- Input的类型(type)
HTML5 新的 Input 类型 HTML5 拥有多个新的表单输入类型.这些新特性提供了更好的输入控制和验证. 本章全面介绍这些新的输入类型: color date datetime datetim ...
- EXTJS4扩展实例:一个调用Ext.picker.Color的颜色选择菜单
运行环境:Extjs4.2.1 运行效果: 调用代码: Ext.require(['MyExtend.Form.Field.ColorField']); Ext.onReady(function() ...
- OBV_X3
{OBV_X3[背景]考虑到OBV_X03在情况1的时候,采用的是寻找波段线的同价K线,但是由于此种情况下必须使用CONST(C)或通过输入参数CONSTCC设定固定值,无法当前K线的CLOSE同时变 ...