springboot LocalDateTime 接参数
时间: 2023-10-18 16:31:18 浏览: 43
在Spring Boot中,如果要接收参数为LocalDateTime类型,有两种方式可以实现。
第一种方式是在前端非JSON请求中,传递参数为时间戳,然后将其转换为LocalDateTime。你可以使用@Configuration注解创建一个配置类,并在该配置类中注册ObjectMapper对象,通过registerModule()方法注册ParameterNamesModule、Jdk8Module和JavaTimeModule模块。同时,你还需要创建一个Formatter<LocalDateTime>对象,在其中实现parse()方法和print()方法,将时间戳转换为LocalDateTime对象。最后,将该配置类添加到Spring Boot应用程序中。
第二种方式是在前端JSON请求中,传递参数为时间戳,然后将其转换为LocalDateTime。你可以创建一个自定义的解析注解@StampToLocalDateTime,并将其应用到参数上。然后,创建一个CustomLocalDateTimeDeserializer类,继承JsonDeserializer<LocalDateTime>,在其中实现deserialize()方法,将时间戳转换为LocalDateTime对象。最后,在Swagger配置类中使用directModelSubstitute()方法将LocalDateTime类型替换为String类型,以支持Swagger对LocalDateTime类型的参数解析。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [springboot 接收LocalDateTime](https://blog.csdn.net/qq_34789577/article/details/109035014)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]