這幾天的開發過程中,需要修改原有同事寫的項目,原項目采用的是springboot寫的后端服務,json序列化使用原始jackson進行,並在配置文件的properties文件中聲明了jackson的一些基本配置
# Json spring.jackson.time-zone=GMT+8 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.property-naming-strategy=SNAKE_CASE spring.jackson.deserialization.READ_UNKNOWN_ENUM_VALUES_AS_NULL=true
起初在沒有加入項目的攔截器LoginInterceptor的時候,按照項目配置文件的配置,postman請求時采用屬性為下划線的形式並不會報錯,而且在序列化時間類型的屬性時也不會出現時間格式化出問題的情況。開始對項目進行改造之后增加了登陸驗證。並在springboot項目啟動的時候增加了Webconfig來向程序注冊攔截器
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
// 關鍵,將攔截器作為bean寫入配置中
@Autowired private LoginInterceptor loginInterceptor;
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor).addPathPatterns("/**"); // 上傳圖片的路徑除外
super.addInterceptors(registry);
}
}
做完這些之后,准備調試程序時發現,用原有的報文請求程序的接口的時候總是報錯。首先是時間類型轉換失敗,報錯信息如下:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize value of type java.util.Date from String "2018-06-12 12:00:00": not a valid representation (error: Failed to parse Date value '2018-06-12 12:00:00': Can not parse date "2018-06-12 12:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2018-06-12 12:00:00": not a valid representation (error: Failed to parse Date value '2018-06-12 12:00:00': Can not parse date "2018-06-12 12:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null)) at [Source: java.io.PushbackInputStream@7688ebdb; line: 1, column: 281] (through reference chain: com.hlt.cloudoak.base.ForeignApplication["order"]->com.hlt.cloudoak.base.ForeignApplication$Order["notification"]->com.hlt.cloudoak.base.ForeignApplication$Order$Notification["date"]->com.hlt.cloudoak.base.ForeignApplication$Order$Notification$HappenDate["notify"])
然后通過一通查資料,最終在Model的屬性上增加了@JsonFormat注解得以解決了這個時間格式化錯誤的問題。
但是事情並沒有完,雖然解決了時間格式化報錯的問題。但是使用下划線形式的Json請求接口依舊行不通,轉化失敗。程序拿不到對應屬性的值。於是又是一頓某度和某歌的翻找。
最后在WebConfig類的實現中增加了代碼,才使得原有的項目依舊得以采用下划線形式的Json,並且時間格式化時也並不會出錯,最終代碼如下:
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
// 關鍵,將攔截器作為bean寫入配置中
@Autowired private LoginInterceptor loginInterceptor;
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor).addPathPatterns("/**"); // 上傳圖片的路徑除外
super.addInterceptors(registry);
}
@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters(converters);
}
}
資料上講,自定義WebMvcConfigur之后,原有properties中的jackson配置會失效。所以必須在自定義實現類中再次對jackson的配置進行補充。查詢資料的過程中,看到有的文章提到需要將注解@EnableWebMvc去掉。但是我們的項目中並不顯式的包含這個注解,相信可能有部分人跟我一樣在看到這個解決方案時並不知道如何對項目進行更改。下面貼一下官方的一段話來解釋一下WebMvcConfigurationSupport類:
public class WebMvcConfigurationSupport
extends Object
implements ApplicationContextAware, ServletContextAware
@EnableWebMvc to an application
@Configuration class. An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add
@Configuration to the subclass and
@Bean to overridden
@Bean methods. For more details see the Javadoc of
@EnableWebMvc.
This class registers the following HandlerMappings:
RequestMappingHandlerMappingordered at 0 for mapping requests to annotated controller methods.HandlerMappingordered at 1 to map URL paths directly to view names.BeanNameUrlHandlerMappingordered at 2 to map URL paths to controller bean names.HandlerMappingordered atInteger.MAX_VALUE-1to serve static resource requests.HandlerMappingordered atInteger.MAX_VALUEto forward requests to the default servlet.
Registers these HandlerAdapters:
RequestMappingHandlerAdapterfor processing requests with annotated controller methods.HttpRequestHandlerAdapterfor processing requests withHttpRequestHandlers.SimpleControllerHandlerAdapterfor processing requests with interface-basedControllers.
Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:
ExceptionHandlerExceptionResolverfor handling exceptions through @ExceptionHandlermethods.ResponseStatusExceptionResolverfor exceptions annotated with @ResponseStatus.DefaultHandlerExceptionResolverfor resolving known Spring exception types
Registers an AntPathMatcher and a UrlPathHelper to be used by:
- the
RequestMappingHandlerMapping, - the
HandlerMappingfor ViewControllers - and the
HandlerMappingfor serving resources
Note that those beans can be configured with a PathMatchConfigurer.
Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default:
- a
ContentNegotiationManager - a
DefaultFormattingConversionService - a
OptionalValidatorFactoryBeanif a JSR-303 implementation is available on the classpath - a range of
HttpMessageConverters depending on the third-party libraries available on the classpath.
