最近的项目没有用到这个,先把自己自学跑通的例子先帖出来,供自己以后参考吧!

如有不对地方望指出!

一、自定义类实现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-自定义消息转换器的更多相关文章

  1. springmvc 类型转换器 自定义类型转换器

    自定义类型转换器的步骤: 1.定义类型转换器 2.类型转换器的注册(在springmvc配置文件处理) 来解决多种日期格式的问题: springmvc 类型转换器 表单数据填错后返回表单页面(接上面的 ...

  2. JavaEE开发之SpringMVC中的自定义消息转换器与文件上传

    上篇博客我们详细的聊了<JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术>,本篇博客依然是JavaEE开发中的内容,我们就来聊一下SpringMVC中的自定义消息转发器 ...

  3. SpringMVC类型转换器、属性编辑器

    对于MVC框架,参数绑定一直觉得是很神奇很方便的一个东西,在参数绑定的过程中利用了属性编辑器.类型转换器 参数绑定流程 参数绑定:把请求中的数据,转化成指定类型的对象,交给处理请求的方法 请求进入到D ...

  4. SpringMVC——消息转换器HttpMessageConverter(转)

    文章转自http://blog.csdn.net/cq1982/article/details/44101293 概述 在SpringMVC中,可以使用@RequestBody和@ResponseBo ...

  5. SpringBoot添加自定义消息转换器

    首先我们需要明白一个概念:springboot中很多配置都是使用了条件注解进行判断一个配置或者引入的类是否在容器中存在,如果存在会如何,如果不存在会如何. 也就是说,有些配置会在springboot中 ...

  6. springboot自定义消息转换器HttpMessageConverter

    在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,底层这种灵活的消息转换机制就是利用HttpMessageCo ...

  7. springmvc 类型转换器 数据回显及提示信息

    处理器的写法: 类型转换器的写法: 类型转换器在springmvc.xml中的配置如下: index.jsp的写法:

  8. springmvc 日期转换器

    package com.xxx.common.controller.converter; import org.joda.time.DateTime; import org.joda.time.for ...

  9. Spring MVC自定义消息转换器(可解决Long类型数据传入前端精度丢失的问题)

    1.前言 对于Long 类型的数据,如果我们在Controller层通过@ResponseBody将返回数据自动转换成json时,不做任何处理,而直接传给前端的话,在Long长度大于17位时会出现精度 ...

  10. springboot自定义消息转换器HttpMessageConverter Spring Boot - 使用Gson替换Jackson

    Jackson一直是springframework默认的json库,从4.1开始,springframework支持通过配置GsonHttpMessageConverter的方式使用Gson. 在典型 ...

随机推荐

  1. Kubernetes容器调度

    Kubernetes的调度器是Kubernetes众多组件的一部分,独立于API服务器之外.调度器本身是可插拔的,任何理解调度器和API服务器之间调用关系的工程师都可以编写定制的调度器.本文后面的介绍 ...

  2. 利用VS正则替换删除文本行首数据等字符

    删除以数字开头的加‘.’的行首序号 ^[0-9][0-9][.]

  3. 格式化NameNode

    $cd /app/hadoop/hadoop-2.2.0/ $./bin/hdfs namenode -format

  4. 统计词语频率保存到xls

    import json import jieba.analyse as anl import xlwt # 获取待统计的文本内容 # 打开文件 f = open('zhilian.json', 'r' ...

  5. 网站用sqlite库,报attempt to write a readonly database,解决方法

    将sqlite数据库文件,设置为users完全控制.重启网站即可!

  6. dp训练

    根据这位大佬的https://www.cnblogs.com/Bunnycxk/p/7360183.html 题目链接:https://www.luogu.org/problemnew/show/P3 ...

  7. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  8. Razor中的@:和语法

    用Razor实现流畅编程 Razor尽量减少编写一个视图模板需要敲入的字符数,实现快速流畅的编程工作流.与大部分模板的语法不同,你不必在HTML中为了明确地标记出服务模块 的开始和结束而中断编程.Ra ...

  9. 2.keras实现-->深度学习用于文本和序列

    1.将文本数据预处理为有用的数据表示 将文本分割成单词(token),并将每一个单词转换为一个向量 将文本分割成单字符(token),并将每一个字符转换为一个向量 提取单词或字符的n-gram(tok ...

  10. TCP状态转换图、滑动窗口、半连接状态、2MSL

    一.TCP状态转换图 下图对排除和定位网络或系统故障时大有帮助,也帮助我们更好的编写Linux程序,对嵌入式开发也有指导意义.    先回顾一下TCP建立连接的三次握手过程,以及关闭连接的四次握手过程 ...