Spring-MVC-配置接收和返回数据的格式
来源:互联网 发布:java 10的n次方 编辑:程序博客网 时间:2024/05/16 06:24
接收页面日期类型数据的转换
自定义类实现Converter接口
public class DateConvert implements Converter<String, Date> { private static final Logger log = LoggerFactory.getLogger(DateConvert.class); @Override public Date convert(String stringDate) { if (StringUtils.isBlank(stringDate)) { return null; } // 这里是关键 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = simpleDateFormat.parse(stringDate); } catch (ParseException e) { } return date; }}
配置xml
加载Bean
<bean id="dateConvert" class="com.iomp.util.DateConvert"/><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="dateConvert"/> </set> </property></bean>
配置MVC
<mvc:annotation-driven conversion-service="conversionService"/>
传递结果中null值转为空字符串的方法
创建类继承ObjectMapper
public class JsonObjectMapper extends ObjectMapper { private static final long serialVersionUID = 1L; public JsonObjectMapper() { super(); // 空值处理为空串 this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException { jg.writeString(""); } }); } }
在SpringMVC配置文件中,把新建的ObjectMapper注入给MappingJackson2HttpMessageConverter
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.xjj.anes.mvc.converter.JsonObjectMapper"></bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
阅读全文
0 0
- Spring-MVC-配置接收和返回数据的格式
- Spring MVC返回JSON格式的数据
- Spring MVC返回JSON格式的数据
- Spring MVC返回XML格式的数据
- Spring MVC返回XML格式的数据
- spring mvc 返回通过配置ResponseBody返回json格式的数据
- Spring MVC 接收与返回 JSON 数据
- 使用spring MVC返回json格式的数据
- spring mvc 返回json格式数据的方式
- spring mvc 对返回json格式数据的支持
- Spring MVC Controller 实现返回XML格式的数据
- spring MVC 配置返回JSON格式的数据模型
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理
- 封装Spring MVC 返回JSON 格式数据
- Spring MVC 返回 xml json pdf 数据的配置方法
- Spring MVC返回json数据的配置方式
- spring MVC 中json格式数据发送与接收
- spring MVC 中json格式数据发送与接收
- 知识付费工具
- fiddler学习笔记
- 获取类类型的三种方式
- from表单的提交
- Android日志系统(Log)
- Spring-MVC-配置接收和返回数据的格式
- numpy深入理解(1) indexing: view or copy
- 本地编译qt源码
- eXtremeDB金融行业解决方案
- Windows的DOS命令查看所有端口和端口对应的程序
- Struts 2 OGNL及案例
- 10.26
- js正则表达式(regular expression)
- 央视牵头4K/UHD标准制定