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

如有不对地方望指出!

一、自定义类实现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. 多线程-interrupt(),isInterrupted(),interrupted()

    背景 由于使用stop方法停止线程非常暴力,可能会导致一系列问题.因此,提出一种温和的方式:请求另外一个先不要在执行了,这就是中断方式. 此外有这样的场景:编写 一个程序,需要暂停一段时间,于是调用T ...

  2. Git 常用命令(转)

    原文:http://www.cnblogs.com/1-2-3/archive/2010/07/18/git-commands.html add by zhj :图是用 思维导图 软件MindMapp ...

  3. MySQL事务隔离级别详解(转)

    原文: http://xm-king.iteye.com/blog/770721 SQL标准对事务定义了4种隔离级别,包括了一些具体规则,用来限定事务内外的哪些改变是可见的,哪些是不可见的.低级别的隔 ...

  4. HIVE表保存的路径

    HIVE表保存的默认路径在${HIVE_HOME}/conf/hive-site.xml配置文件的hive.metastore.warehouse.dir属性指定

  5. 【Jmeter】如何通过文件导入方式对用户名和密码进行参数化设置

    JMeter 参数化 注意:param和data body只能用一个.所有任何一个里面有内容,切换都会报错,这不是问题,jmeter是这么设计的 方法一:通过添加CSV Data Set Config ...

  6. 带分数dfs+剪枝优化

    #include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>using name ...

  7. sql server中的日期详解使用(convert)

    转自:http://blog.csdn.net/hehe520347/article/details/48496853 有个字段值例如2012-07-02 00:00:00.000 转化成 2012- ...

  8. abap开发中update module 的创建和使用

    一.update module 的创建和使用 最近遇到这样一个需求,需要先删除(delete)表中的数据,再将传递过来的新数据添加(modify)到表中. 但是如果下面modify的时候出现错误,使用 ...

  9. [LeetCode] 595. Big Countries_Easy tag: SQL

    There is a table World +-----------------+------------+------------+--------------+---------------+ ...

  10. Selenium Webdriver——操作隐藏的元素(四)

    页面上弹出的对话框是自动化测试经常会遇到的一个问题:很多情况下对话框是一个iframe,如上一节中介绍的例子,处理起来稍微有点麻烦:但现在很多前端框架的对话框是div 形式的,这就让我们的处理变得十分 ...