LocalTime转换为Json – 无法更改时间格式

huangapple go评论69阅读模式
英文:

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&lt;TimePeroid&gt;, Serializable {

    private static final long serialVersionUID = -65707223409901256L;
        
    @Expose
    //@JsonFormat(pattern=&quot;HH:mm:ss&quot;)
    private LocalTime start;
        
    @Expose
    //@JsonFormat(pattern=&quot;HH:mm:ss&quot;)
    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

{
&quot;start&quot; : {&quot;hour&quot;:6,&quot;minute&quot;:0,&quot;second&quot;:0,&quot;nano&quot;:0},  
&quot;end&quot;:{&quot;hour&quot;:18,&quot;minute&quot;:0,&quot;second&quot;:0,&quot;nano&quot;:0}  
}

But I need it like this

{  
&quot;start&quot;:&quot;06:00:00&quot;,  
&quot;end&quot;:&quot;18:00:00&quot;  
}

What I've tried until now

Solution 1

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

Solution 2

@JsonFormat(pattern=&quot;HH:mm:ss&quot;)

Solution 3

ObjectMapper mapperObj = new ObjectMapper();
    			mapperObj.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    			DateFormat df = new SimpleDateFormat(&quot;HH:mm&quot;);
    			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

&lt;dependency&gt;
    &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
    &lt;artifactId&gt;jackson-datatype-jsr310&lt;/artifactId&gt;
    &lt;version&gt;2.6.0&lt;/version&gt;
&lt;/dependency&gt;

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 -&gt; {
    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")

huangapple
  • 本文由 发表于 2020年10月6日 14:20:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64220324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定