第一种:
-
import com.fasterxml.jackson.annotation.JsonFormat;
-
import org.springframework.format.annotation.DateTimeFormat;
-
@DateTimeFormat
-
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
-
@ApiModelProperty(value = "时间", example = "2020-4-5 10:30:55")
-
private Date time;
第二种:
在yml中添加
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
第二种全局配置失效的原因
项目中有类
extends WebMvcConfigurationSupport
https://www.jb51.net/article/174766.htm
二种解决方法:
1.
@Configuration public class Configurer extends WebMvcConfigurationSupport{ @Autowired HttpInterceptor httpInterceptor; //定义时间格式转换器 @Bean public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); converter.setObjectMapper(mapper); return converter; } //添加转换器 @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //将我们定义的时间格式转换器添加到转换器列表中, //这样jackson格式化时候但凡遇到Date类型就会转换成我们定义的格式 converters.add(jackson2HttpMessageConverter()); } @Override protected void addInterceptors(InterceptorRegistry registry) { // TODO Auto-generated method stub registry.addInterceptor(httpInterceptor).addPathPatterns("/**"); super.addInterceptors(registry); } }
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH") .allowCredentials(true).maxAge(3600); } }
2.
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("swagger-ui.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); /** * 序列换成json时,将所有的long变成string * 因为js中得数字类型不能包含所有的java long值 */ SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); //解决json中包含实体类中没有的字段时异常 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); jackson2HttpMessageConverter.setObjectMapper(objectMapper); converters.add(jackson2HttpMessageConverter); } }
如果有帮助到你的话,请记得关注一波
博主强烈推荐:https://blog.csdn.net/persistencegoing/article/details/84376427
希望大家关注我一波,防止以后迷路,有需要的可以加群讨论互相学习java ,学习路线探讨,经验分享与java求职
群号:721 515 304