简体   繁体   中英

Spring 4 ,the custom messageConverter don`t work

I want to use @RestController annotation & jackson2, but the response JSON (include java.util.Date) always return Timestamp;I did the following things, but it dose not work...

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean id="customJsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="customObjectMapper"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>



public class CustomObjectMapper extends ObjectMapper{


public CustomObjectMapper(){
    this.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    this.setDateFormat(df);
}

}

@RestController
@RequestMapping(value = "/reports")
public class ReportController extends...

debug and find there is only defualt 5 MessageConverters... the screenshoot

This is how you could do it using annotations

@Configuration
public class JacksonConfiguration extends WebMvcConfigurerAdapter {

    private final CustomObjectMapper mapper;

    @Autowired
    public JacksonConfiguration(CustomObjectMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter(mapper));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM