英文:
LocalTime into Json - unable to change time format
问题
以下是翻译好的内容:
我有一个类如下所示:
public class TimePeroid implements Comparable<TimePeroid>, Serializable {
    private static final long serialVersionUID = -65707223409901256L;
        
    @Expose
    //@JsonFormat(pattern="HH:mm:ss")
    private LocalTime start;
        
    @Expose
    //@JsonFormat(pattern="HH:mm:ss")
    private LocalTime end;
    //getter setter
}
在响应时,当我使用ObjectMapper将其转换为JSON字符串时,我得到如下的JSON字符串:
{
"start" : {"hour":6,"minute":0,"second":0,"nano":0},  
"end":{"hour":18,"minute":0,"second":0,"nano":0}  
}
但我需要它像这样:
{  
"start":"06:00:00",  
"end":"18:00:00"  
}
我到目前为止尝试过的方法如下:
解决方案1
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
解决方案2
@JsonFormat(pattern="HH:mm:ss")
解决方案3
ObjectMapper mapperObj = new ObjectMapper();
mapperObj.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
DateFormat df = new SimpleDateFormat("HH:mm");
mapperObj.setDateFormat(df);
但都没有起作用。请在这个问题上帮助我。
英文:
I have a class like the following
public class TimePeroid implements Comparable<TimePeroid>, Serializable {
    private static final long serialVersionUID = -65707223409901256L;
        
    @Expose
    //@JsonFormat(pattern="HH:mm:ss")
    private LocalTime start;
        
    @Expose
    //@JsonFormat(pattern="HH:mm:ss")
    private LocalTime end;
    //getter setter
}
At response time when I convert it into a JSON string with the help of ObjectMapper, I get a JSON string like below
{
"start" : {"hour":6,"minute":0,"second":0,"nano":0},  
"end":{"hour":18,"minute":0,"second":0,"nano":0}  
}
But I need it like this
{  
"start":"06:00:00",  
"end":"18:00:00"  
}
What I've tried until now
Solution 1
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
Solution 2
@JsonFormat(pattern="HH:mm:ss")
Solution 3
ObjectMapper mapperObj = new ObjectMapper();
    			mapperObj.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    			DateFormat df = new SimpleDateFormat("HH:mm");
    			mapperObj.setDateFormat(df);
None of them works. please help me on this
答案1
得分: 3
你是否已将JavaTimeModule包含在你的Jackson配置中?
一个相关的SO问题和答案在这里:https://stackoverflow.com/a/32202201/134894
从那个答案中复制/粘贴:
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>
以及
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
一旦你注册了这个模块,你也可以潜在地配置默认的LocalDate格式,通过注册一个序列化程序。
SimpleModule module = new SimpleModule();
module.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
mapper.registerModule(module);
实际上,我看到你正在使用Spring Boot...所以这可能更符合那个环境:
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
  return builder -> {
    builder.serializers(new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
    builder.deserializers(new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME));
    builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
 };
}
英文:
Have you included JavaTimeModule in your Jackson configuration?
A related SO question & answer here https://stackoverflow.com/a/32202201/134894
To copy/paste from that answer
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>
and
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
Once you have the module registered, you could also potentially configure the default LocalDate formatting, by registering a serializer.
SimpleModule module = new SimpleModule();
module.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
mapper.registerModule(module);
Actually, I see you're using Spring Boot.. so this might be more idiomatic for that environment
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
  return builder -> {
    builder.serializers(new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
    builder.deserializers(new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME));
    builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
 };
}
答案2
得分: 0
你需要使用"KK",而不是"HH"。
代码如下:
@JsonFormat(pattern="KK:mm")
英文:
You need to use "KK, not "HH"
Code Below:
@JsonFormat(pattern="KK:mm")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论