- Spring Boot 2.0 Cookbook(Second Edition)
- Alex Antonov
- 292字
- 2021-06-24 19:24:42
How it works...
As you can see, Spring gives us multiple ways of achieving the same thing and it all depends on our preference or particular details of the implementation.
We covered three different ways of adding HttpMessageConverter to our application. So what is the difference, one might ask?
Declaring HttpMessageConverter as @Bean is the quickest and simplest way of adding a custom converter to the application. It is similar to how we added servlet filters in an earlier example. If Spring detects a bean of the HttpMessageConverter type, it will add it to the list automatically. If we did not have a WebConfiguration class that implements WebMvcConfigurer, it would have been the preferred approach.
When the application needs to define a more precise control over the settings, like interceptors, mappings, etc, it is best to use WebMvcConfigurer implementation to configure those, as it would be more consistent to override the configureMessageConverters method and add our converter to the list. As there can be multiple instances of WebMvcConfigurers, which could be either added by us or via the auto-configuration settings from various Spring Boot starters, there is no guarantee that our method can get called in any particular order.
If we need to do something even more drastic such as removing all the other converters from the list or clearing it of duplicate converters, this is where overriding extendMessageConverters comes into play. This method gets invoked after all the WebMvcConfigurers get called for configureMessageConverters and the list of converters is fully populated. Of course, it is entirely possible that some other instance of WebMvcConfigurer could override extendMessageConverters as well; but the chances of this are very low so you have a high degree of having the desired impact.
- 在你身邊為你設計Ⅲ:騰訊服務設計思維與實戰
- Greenplum:從大數據戰略到實現
- 醫療大數據挖掘與可視化
- 數據化網站運營深度剖析
- UDK iOS Game Development Beginner's Guide
- 大數據時代下的智能轉型進程精選(套裝共10冊)
- 數據驅動設計:A/B測試提升用戶體驗
- 數據庫技術及應用教程
- Augmented Reality using Appcelerator Titanium Starter
- 大數據數學基礎(Python語言描述)
- Filecoin原理與實現
- AndEngine for Android Game Development Cookbook
- 云原生架構:從技術演進到最佳實踐
- 深入理解Flink:實時大數據處理實踐
- Access 2010數據庫應用技術教程(第二版)