解决LocalDateTime传值JSON格式化问题
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
LocalDateTime是JDK8中提供的新功能,极大的优化了原生日期时间类的使用。但是第一次使用该类可能会在传值过程中出现格式化的小问题(如:JSON无法解析前端所传格式,序列化时LocalDateTime成为数组等),以下提供简单的解决方案。
推荐方法:
在WebMvcConfigurer实现类下完成以下两步
1). 注册一个Converter<String, LocalDateTime>
实现类,其作用是处理于url
所携带的参数上(如:@RequestParam
、@PathVariable
)的LocalDateTime参数;
2). 增加一个序列化、反序列化器,作用为处理实体类
的的LocalDateTime属性。
P.S. 经过多轮测试,得出了以上方法,由于该方法需要以上两个步骤,作为完美主义者,我曾尝试用一个步骤解决,但并未如愿以偿,若你有更好的方法,还望不吝赐教,先行谢过。
源码如下:
// import ...
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 自定义String转LocalDateTime方法,此方法将会作用于url所携带的参数上
*/
static class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String s) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(s, formatter);
}
}
/**
* 将上述自定义方法进行添加
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToLocalDateTimeConverter());
}
/**
* 增加序列化与反序列化器,它们将作用于实体类的LocalDateTime属性。
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(pattern));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(pattern));
objectMapper.registerModule(module);
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}
}
其它方法:
url参数:
给参数加上@DateTimeFormat
(此注解来自Spring
,无需引入其他包),在pattern
中标注约定好的格式即可。
public void Test(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
...
}
实体类:
在实体类中的属性上添加@JsonFormat
(此注解来自Jackson
,该包被Spring
所依赖,无需导入),同样在pattern
中标注好格式。
@Data
public class Demo {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime time;
}
由于这种方法需要对代码中所有的LocalDateTime都进行标注,相对麻烦,且耦合度高,所以不作推荐。
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 4 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 4 天前
更多推荐
已为社区贡献8条内容
所有评论(0)