Spring @ResponseBody Jackson JsonSerializer with JodaTime

后端 未结 5 870
我在风中等你
我在风中等你 2020-12-03 11:26

I have below Serializer for JodaTime handling:

public class JodaDateTimeJsonSerializer extends JsonSerializer {

    private static final Str         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 11:53

    Although you can put an annotation for each date field, is better to do a global configuration for your object mapper. If you use jackson you can configure your spring as follow:

    
    
    
    
    

    For CustomObjectMapper:

    public class CustomObjectMapper extends ObjectMapper {
    
        public CustomObjectMapper() {
            super();
            configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
            setDateFormat(new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)"));
        }
    }
    

    Of course, SimpleDateFormat can use any format you need.

提交回复
热议问题