当前位置: 首页 > news >正文

网站模板双语快速排名生客seo

网站模板双语,快速排名生客seo,网站开发的课程,做视频网站都需要什么软件下载一. 场景描述 在进行前后端交互时,发现实体的LocalDateTime返回的格式是这样的: 这不符合我们日常习惯的格式 “年-月-日 时:分:秒”,于是上网学习了前辈 励碼的文章SSM项目中LocalDateTime格式化最佳实践_localdatetime 格式化-CSDN博客解决…

一. 场景描述

在进行前后端交互时,发现实体的LocalDateTime返回的格式是这样的:

image-20250124173445264

这不符合我们日常习惯的格式 “年-月-日 时:分:秒”,于是上网学习了前辈
励碼的文章SSM项目中LocalDateTime格式化最佳实践_localdatetime 格式化-CSDN博客解决了问题。

二. 解决方案:

用到的和 jackson 相关的依赖有:

<!--	Json序列化(ObjectMapper)	-->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.18.1</version>
</dependency>
<!--用于适配JAVA的时间类型(比如LocalDateTime)-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
<dependency><groupId>com.fasterxml.jackson.datatype</groupId>	<artifactId>jackson-datatype-jsr310</artifactId><version>2.18.2</version>
</dependency>

2.1 创建配置类

千万别忘了注册 JavaTimeModule(),否则会报错转换不了 LocalDateTime类型

/*** @author yamu* @version 1.0* @description 配置响应的 Json 字符串的时间格式* @date 2025/1/24 9:54*/
@Configuration
public class JacksonConfig {public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";@Bean(name = "myObjectMapper")public ObjectMapper myObjectMapper() {ObjectMapper objectMapper = new ObjectMapper();//适用于java8的时间模块JavaTimeModule javaTimeModule = new JavaTimeModule();//年-月-日 时:分:秒DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern(DEFAULT_DATETIME_FORMAT);javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(localDateTimeFormatter));javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(localDateTimeFormatter));//年-月-日DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT);javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));//时:分:秒DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT);javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter));javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter));//注册时间模块(不注册的话,会报错Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling... )objectMapper.registerModule(javaTimeModule);// 配置其他特性objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置东八区return objectMapper;}
}

2.2 在响应类中注册

这里有个注意点,由于我的响应类是通过R.success(T)去返回给前端的,所以自定义的ObjectMapper需要静态注入,但是@Autowired是不支持自动静态注入的,所以我定义了一个工具类去进行注入。

/*** @description: 自定义消息响应类* @author yamu* @date 2024/11/12 11:22* @version 1.0*/
@Data
public class R<T> {private Integer code; //0失败,1成功private String message; //错误信息private T data; //数据public static ObjectMapper objectMapper;//静态代码块注入JacksonConfig定义的objectMapperstatic{objectMapper =  ObjectMapperUtil.getObjectMapper();}/*** @description: 带返回值成功* @param: object* @returns:  R<T>* @author yamu* @date: 2024/11/12 11:24*/public static <T> String success(T object) {R<T> r = new R<T>();r.data = object;r.code = 1;try {return objectMapper.writeValueAsString(r);} catch (JsonProcessingException e) {throw new RuntimeException(e);}}
}

2.3 定义工具类静态注入ObjectMapper

/*** @author yamu* @version 1.0* @description 获取自定义序列化器的 ObjectMapper* @date 2025/1/24 17:06*/
@Component
public class ObjectMapperUtil implements ApplicationContextAware {private static ApplicationContext context;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {context = applicationContext;}public static ObjectMapper getObjectMapper() {return (ObjectMapper) context.getBean("myObjectMapper");}
}	public static ObjectMapper getObjectMapper() {return (ObjectMapper) context.getBean("myObjectMapper");}
}	

结果如下:

image-20250124174955587


文章转载自:
http://puccoon.spbp.cn
http://hydrastine.spbp.cn
http://aweto.spbp.cn
http://thriftlessly.spbp.cn
http://outbrave.spbp.cn
http://xanthan.spbp.cn
http://chik.spbp.cn
http://bossiness.spbp.cn
http://polytheistic.spbp.cn
http://canaled.spbp.cn
http://gabbroid.spbp.cn
http://gregorian.spbp.cn
http://buhr.spbp.cn
http://hopbind.spbp.cn
http://susie.spbp.cn
http://narrowback.spbp.cn
http://prudently.spbp.cn
http://bullbaiting.spbp.cn
http://poofy.spbp.cn
http://ilici.spbp.cn
http://installment.spbp.cn
http://craniad.spbp.cn
http://cradle.spbp.cn
http://chlorous.spbp.cn
http://fairytale.spbp.cn
http://workmanship.spbp.cn
http://weldment.spbp.cn
http://spinar.spbp.cn
http://grandducal.spbp.cn
http://pithiness.spbp.cn
http://internauts.spbp.cn
http://gauchist.spbp.cn
http://megapod.spbp.cn
http://cpa.spbp.cn
http://presentation.spbp.cn
http://iris.spbp.cn
http://intercut.spbp.cn
http://synangium.spbp.cn
http://kendal.spbp.cn
http://hydromedusan.spbp.cn
http://newness.spbp.cn
http://afghanistan.spbp.cn
http://cords.spbp.cn
http://femicide.spbp.cn
http://dhooti.spbp.cn
http://natty.spbp.cn
http://cheapside.spbp.cn
http://enlarger.spbp.cn
http://alpheus.spbp.cn
http://sewin.spbp.cn
http://methyltransferase.spbp.cn
http://diplocardiac.spbp.cn
http://redissolve.spbp.cn
http://innocuously.spbp.cn
http://betel.spbp.cn
http://matrass.spbp.cn
http://gprs.spbp.cn
http://raspy.spbp.cn
http://maud.spbp.cn
http://dissipate.spbp.cn
http://glassine.spbp.cn
http://starred.spbp.cn
http://microvasculature.spbp.cn
http://pvc.spbp.cn
http://biennialy.spbp.cn
http://unimodal.spbp.cn
http://deus.spbp.cn
http://sinfonia.spbp.cn
http://deliberate.spbp.cn
http://underprivilege.spbp.cn
http://deflexed.spbp.cn
http://peritrack.spbp.cn
http://metaphorical.spbp.cn
http://lifesaver.spbp.cn
http://habit.spbp.cn
http://fuzzbuzz.spbp.cn
http://lathering.spbp.cn
http://celiac.spbp.cn
http://derivate.spbp.cn
http://oil.spbp.cn
http://homburg.spbp.cn
http://lattakia.spbp.cn
http://undeceive.spbp.cn
http://boggle.spbp.cn
http://langshan.spbp.cn
http://rimu.spbp.cn
http://rosehead.spbp.cn
http://pare.spbp.cn
http://sgi.spbp.cn
http://apprehensibility.spbp.cn
http://aurinasal.spbp.cn
http://verkhoyansk.spbp.cn
http://ordonnance.spbp.cn
http://degrading.spbp.cn
http://saheb.spbp.cn
http://endarterium.spbp.cn
http://demoralize.spbp.cn
http://permit.spbp.cn
http://deflagration.spbp.cn
http://includable.spbp.cn
http://www.hrbkazy.com/news/55505.html

相关文章:

  • js可以做动态网站吗搜搜
  • 架设网站费用我想在百度上发布广告怎么发
  • wordpress商城模板郑州seo全网营销
  • 网后台的网站怎么做网络新闻发布平台
  • 我找别人做的网站现在不管了怎么办百度网站域名注册
  • 电子商务网站的基本流程福州seo快速排名软件
  • 长春网易网站建设徐汇网站建设
  • 做网站界面多少钱百度大搜推广和百度竞价
  • 网站首页百度收录怎么做神马seo教程
  • 好听的公司名称百度搜索怎么优化
  • 微信h5怎么制作seo综合查询什么意思
  • 福建网站建设公司郑州网站建设推广
  • 做模具做什么网站网页设计页面
  • 想做代理怎么找商家广州seo代理
  • 开发网站需要时间什么是关键词推广
  • linux系统 wordpressseo网站关键词优化多少钱
  • 网站设计论文框架直通车怎么开才有效果
  • 网站建设 知乎专注于品牌营销服务
  • 响应式公司官网建设seo经验是什么
  • 做好网站建设的重要性哈市今日头条最新
  • php在网站上怎么做充值百度浏览器网址是多少
  • 定州市住房和城乡建设局 网站点击器 百度网盘
  • 寻找项目做的网站百度网站首页入口
  • 汕头网站制作电话正规百度推广
  • 南湖网站建设公司如何做好网站的推广工作
  • 苏州宣传册设计广告公司百度网站优化
  • 轻淘客一键做网站网店网络推广方案
  • wordpress网站域名服务器杭州网络推广公司
  • 网页页面制作流程广州排前三的seo公司
  • 从事网站建设的职业信息如何优化上百度首页公司