springboot---->springboot中的格式化(一)
这里面我们简单的学习一下springboot中关于数据格式化的使用。我以为你不是个好人,没想到你连个坏人都不是。
springboot中的格式化
我们的测试环境是springboot,一个将字符串格式化成时间的例子。代码结构如下:

一、定义我们的字符串格式化器
package com.linux.huhx.learn.format; import org.springframework.format.Formatter;
import org.springframework.stereotype.Component; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale; /**
* @Author: huhx
* @Date: 2017-12-15 下午 4:16
* @Desc: formatter的学习
*/
@Component
public class DateFormatter implements Formatter<Date> {
private SimpleDateFormat dateFormat; @Override
public Date parse(String text, Locale locale) throws ParseException {
System.out.println("parse input text: " + text);
System.out.println("parse date: " + dateFormat.parse(text));
return dateFormat.parse(text);
} public DateFormatter() {
dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
dateFormat.setLenient(false);
} @Override
public String print(Date object, Locale locale) {
System.out.println("print method." + object);
return dateFormat.format(object);
}
}
二、我们的测试控制器类
package com.linux.huhx.learn.format; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; /**
* @Author: huhx
* @Date: 2017-12-15 下午 4:24
*/ @RestController
@RequestMapping("/formatter")
public class DateFormatterAction { @PostMapping("/date")
public Date formatterStringToDate(Date date) {
System.out.println("action method: " + date.toString());
return date;
}
}
使用postman发送post请求:http://localhost:9998/formatter/date。

返回的数据:以下的时间戳是spring经过转换生成的
控制台打印数据:
parse input text:
parse date: Fri Dec :: CST
action method: Fri Dec :: CST
友情链接
springboot---->springboot中的格式化(一)的更多相关文章
- SpringBoot中时间格式化的5种方法!
在我们日常工作中,时间格式化是一件经常遇到的事儿,所以本文我们就来盘点一下 Spring Boot 中时间格式化的几种方法. 时间问题演示 为了方便演示,我写了一个简单 Spring Boot 项 ...
- springboot 项目中获取默认注入的序列化对象 ObjectMapper
在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...
- SpringBoot返回date日期格式化
SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差 问题描述 在Spring Boot项目中,使用@RestController注解,返回的java对象中 ...
- 国际化的实现i18n--错误码国际化以及在springboot项目中使用
国际化 ,英文叫 internationalization 单词太长 ,又被简称为 i18n(取头取尾中间有18个字母); 主要涉及3个类: Locale用来设置定制的语言和国家代码 Resource ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- 在SpringBoot项目中添加logback的MDC
在SpringBoot项目中添加logback的MDC 先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...
- 带着新人学springboot的应用06(springboot+RabbitMQ 中)
上一节说了这么多废话,看也看烦了,现在我们就来用鼠标点点点,来简单玩一下这个RabbitMQ. 注意:这一节还是不用敲什么代码,因为上一节我们设置了那个可视化工具,我们先用用可视化工具熟悉一下流程. ...
- 自身使用的springboot项目中比较全的pom.xml
在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...
- 记一次SpringBoot 开发中所遇到的坑和解决方法
记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...
- springboot配置文件中使用当前配置的变量
在开发中,有时我们的application.properties某些值需要重复使用,比如配置redis和数据库或者mongodb连接地址,日志,文件上传地址等,且这些地址如果都是相同或者父路径是相同的 ...
随机推荐
- Python 3.x标准模块库目录
出处:http://blog.csdn.net/sadfishsc/article/details/10390065 文本 1. string:通用字符串操作 2. re:正则表达式操作 3. dif ...
- ie10以上媒体查询 css
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { }
- [转]Apache 监听端口失败,selinux惹的祸
原文在此 CentOS 下启动Httpd 失败,报 (13)Permission denied: make_sock: could not bind to address [::]:8000 因为 小 ...
- getIsDebuggable
/* * if set android:debuggable="true" in Manifest, return true. * if set android:debugga ...
- Linux系统安装workerman,启动wss 服务
安装workerman其实很简单,只要会简单的linux口令就可以搞定, 这里我给大家演示一下如何安装workerman 进入终端的过程就不用演示了吧... 输入root及密码进入终端后找到站点根目录 ...
- 使用pycharm,追求最优的代码。
1.最近追求的是代码0警告,没有任何提示. 怎么追求这样的目标,不需要再去单独使用pylint和flake8这些玩意,只需要看pycharm右边编辑区的竖向滚动条的黄色就可以了. 2. 比较糟糕的就是 ...
- python--面向对象--14
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ Python 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Pyth ...
- Layui:设置select下拉框自动选中某项
1.问题:layUI,在做编辑功能有下拉框数据时,需要初始化选中某个值,layUI官网没有相关api,可能我比较笨没找到 2.解决思路:出发点击事件 3.分析dom树结构,出发dl点击事件 方法: v ...
- scala spray 概念性内容的总结
spray 是基于 akka 的轻量级 scala 库,可用于编写 REST API 服务.了解 spray 的 DSL 后可以在很短的时间内写出一个 REST API 服务,它的部署并不需要 tom ...
- win10屏幕投影
不管是做设计还是看电影玩游戏编代码,多屏幕总是可以带来方便的.屏幕投影之前需要连接屏幕:通过VGA线连接.通过HDMI线连接.或者通过无线连接. 介绍Win10实现屏幕投影设置:https://jin ...