Spring @ResponseBody Jackson JsonSerializer with JodaTime

后端 未结 5 866
我在风中等你
我在风中等你 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:39

    If you simply have the Jackson JARs on your classpath, and return a @ResponseBody, Spring will automatically convert the Model object to JSON. You don't need to annotate anything in the Model to get this to work.

    0 讨论(0)
  • 2020-12-03 11:50

    If you are using Spring Boot, try this in application.yml :

    spring:
        jackson:
           date-format: yyyy-MM-dd
           time-zone: Asia/Shanghai
           joda-date-time-format: yyyy-MM-dd
    
    0 讨论(0)
  • 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:

    <bean id="jacksonObjectMapper" class="com.company.CustomObjectMapper" />
    
    <bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
        factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" >
    </bean>
    

    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.

    0 讨论(0)
  • 2020-12-03 11:56

    Same using JavaConfig of Spring 3:

    @Configuration
    @ComponentScan()
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter
    {
    
        @Override
        public void configureMessageConverters(final List<HttpMessageConverter<?>> converters)
        {
            converters.add(0, jsonConverter());
        }
    
        @Bean
        public MappingJacksonHttpMessageConverter jsonConverter()
        {
            final MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
            converter.setObjectMapper(new CustomObjectMapper());
    
            return converter;
        }
    }
    
    0 讨论(0)
  • 2020-12-03 11:59

    @Moesio pretty much got it. Here's my config:

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven/>
    
    <!-- Instantiation of the Default serializer in order to configure it -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterConfigurer" init-method="init">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="objectMapper" ref="jacksonObjectMapper" />
                </bean>
            </list>
        </property>
    </bean>
    
    <bean id="jacksonObjectMapper" class="My Custom ObjectMapper"/>
    
    <bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
        factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
    

    The bit that got me is that <mvc:annotation-driven/> makes its own AnnotationMethodHandler and ignores the one you make manually. I got the BeanPostProcessing idea from http://scottfrederick.blogspot.com/2011/03/customizing-spring-3-mvcannotation.html to configure the one that gets used, and voilà! Works like a charm.

    0 讨论(0)
提交回复
热议问题