我想使用@RestController注释& jackson2,但是响应JSON(包括java.util.Date)总是返回Timestamp;我做了以下事情,但它不起作用......
<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...
调试并发现只有5个MessageConverters的defualt ... the screenshoot
答案 0 :(得分:0)
这是使用注释
的方法@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));
}
}