这里面我们简单的学习一下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中的格式化(一)的更多相关文章

  1. SpringBoot中时间格式化的5种方法!

    在我们日常工作中,时间格式化是一件经常遇到的事儿,所以本文我们就来盘点一下 Spring Boot 中时间格式化的几种方法. ​ 时间问题演示 为了方便演示,我写了一个简单 Spring Boot 项 ...

  2. springboot 项目中获取默认注入的序列化对象 ObjectMapper

    在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...

  3. SpringBoot返回date日期格式化

    SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差 问题描述 在Spring Boot项目中,使用@RestController注解,返回的java对象中 ...

  4. 国际化的实现i18n--错误码国际化以及在springboot项目中使用

    国际化 ,英文叫 internationalization 单词太长 ,又被简称为 i18n(取头取尾中间有18个字母); 主要涉及3个类: Locale用来设置定制的语言和国家代码 Resource ...

  5. SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL

    1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...

  6. 在SpringBoot项目中添加logback的MDC

    在SpringBoot项目中添加logback的MDC     先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...

  7. 带着新人学springboot的应用06(springboot+RabbitMQ 中)

    上一节说了这么多废话,看也看烦了,现在我们就来用鼠标点点点,来简单玩一下这个RabbitMQ. 注意:这一节还是不用敲什么代码,因为上一节我们设置了那个可视化工具,我们先用用可视化工具熟悉一下流程. ...

  8. 自身使用的springboot项目中比较全的pom.xml

    在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...

  9. 记一次SpringBoot 开发中所遇到的坑和解决方法

    记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...

  10. springboot配置文件中使用当前配置的变量

    在开发中,有时我们的application.properties某些值需要重复使用,比如配置redis和数据库或者mongodb连接地址,日志,文件上传地址等,且这些地址如果都是相同或者父路径是相同的 ...

随机推荐

  1. 下载android源码

    http://source.android.com Step 1.按照http://source.android.com/source/initializing.html配置好android编译环境 ...

  2. nginx+Uwsgi+Django总结与分析

    配置与调试nginx与uwsgi 參考: 1.uWSGI其三:uWSGI搭配Nginx使用 2.学习VirtualEnv和Nginx+uwsgi用于django项目部署 3.部署备忘 4.nginx+ ...

  3. Eclipse的SVN插件移动中文名称文件提示org.tigris.subversion.javahl.ClientException: Bogus URL

    今天一个同事使用Eclipse的SVN插件,在"SVN资源库"视图,移动一个中文名称的文件,提示org.tigris.subversion.javahl.ClientExcepti ...

  4. 转载: 让我们聊聊Erlang的nif中资源的安全释放

    让我们聊聊Erlang的nif中资源的安全释放 http://my.oschina.net/u/236698/blog/479221

  5. vegan 包进行Adonis 分析

    Adonis 分析 是基于距离矩阵的多变量方差置换分析, 代码示例: 默认使用bray 距离来计算样本间的距离矩阵 参考资料: https://www.rdocumentation.org/packa ...

  6. PolymiRTS 数据库- miRNA SNP数据库

    背景: miRNA通过和mRNA的3'UTR区结合,导致mRNA讲解或者抑制mRNA翻译,从而实现转录后调控的作用: 如果在miRNA和 mRNA的结合区域,发生了snp,就可能会影响miRNA和mR ...

  7. VS2010 正则批量替换头文件路径

        最近在项目实践中,需要统一对工程头文件进行重构,具体要求是,将之前 #include "../../abc/def.h" 类似的头文件引用路径 替换为#include &q ...

  8. 网络上可供测试的Web Service

    网络上可供测试的Web Service 腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService ...

  9. Java实习生面试题整理

    一.数据类型 包装类型 八个基本类型: boolean/1 byte/8 char/16 short/16 int/32 float/32 long/64 double/64 基本类型都有对应的包装类 ...

  10. jekins job configure找不到remote trigger(script)

    今天想测试一下,remote的方式启动一个job,但是在“构建触发器”一栏根本找不到remote trigger,很惊讶的是在网上所有的doc或者demo里都是有这个选项的. 最后,终于找到了原因: ...