Customizing HttpMessageConverters with Spring Boot and Spring MVC
Exposing a REST based endpoint for a Spring Boot application or for that matter a straight Spring MVC application is straightforward, the following is a controller exposing an endpoint to create an entity based on the content POST'ed to it:
@RestController @RequestMapping("/rest/hotels") public class RestHotelController { .... @RequestMapping(method=RequestMethod.POST) public Hotel create(@RequestBody @Valid Hotel hotel) { return this.hotelRepository.save(hotel); } }
Internally Spring MVC uses a component called a HttpMessageConverter to convert the Http request to an object representation and back.
A set of default converters are automatically registered which supports a whole range of different resource representation formats - json, xml for instance.
Now, if there is a need to customize the message converters in some way, Spring Boot makes it simple. As an example consider if the POST method in the sample above needs to be little more flexible and should ignore properties which are not present in the Hotel entity - typically this can be done by configuring the JacksonObjectMapper , all that needs to be done with Spring Boot is to create a new HttpMessageConverter bean and that would end up overriding all the default message converters, this way:
@Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); jsonConverter.setObjectMapper(objectMapper); return jsonConverter; }
This works well for a Spring-Boot application, however for straight Spring MVC applications which do not make use of Spring-Boot, configuring a custom converter is a little more complicated - the default converters are not registered by default and an end user has to be explicit about registering the defaults:
@Configuration public class WebConfig extends WebMvcConfigurationSupport { @Bean public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); jsonConverter.setObjectMapper(objectMapper); return jsonConverter; } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(customJackson2HttpMessageConverter()); super.addDefaultHttpMessageConverters(); } }
Here WebMvcConfigurationSupport provides a way to more finely tune the MVC tier configuration of a Spring based application. In the configureMessageConverters method, the custom converter is being registered and then an explicit call is being made to ensure that the defaults are registered also. A little more work than for a Spring-Boot based application.
相关推荐
4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. Getting Started 8. Introducing Spring Boot 9. System Requirements 9.1. Servlet ...
Spring.Boot.in.Action.2015.12.pdfFor online information and ordering of this and other manning books, please visit www.manning.com.thepublisheroffersdiscountsonthisbookwhenorderedinquantity For more ...
Developing applications with Spring Boot Customizing configuration files Creating RESTful Web Services API Documentation Using Swagger 2 together with Spring Boot Testing API with Swagger UI Spring ...
Chapter 11 – Validation and data binding in Spring Web MVC Chapter 12 –Developing RESTful web services using Spring Web MVC Chapter 13 – More Spring Web MVC – internationalization, file upload and...
Spring Boot 文档 java开发必备 Spring Boot 文档 java开发必备
5.1. Getting started with Spring MVC 5.1.1. Following the life of a request 5.1.2. Setting up Spring MVC 5.1.3. Introducing the Spittr application 5.2. Writing a simple controller 5.2.1. Testing the ...
Customizing Bootstrap 3 with LESS -- Ex_Files_Boot_Less
MySpringBoot That is a project for learning the springBoot 31.5. Using H2’s Web Console 31.5.1. Changing the H2 Console’s Path 31.6. Using jOOQ 31.6.1. Code Generation 31.6.2. Using DSLContext 31.6....
Embedded Android Porting, Extending, and Customizing 2013最新版
This book will also take you one step forward by introducing you to spring 5 and spring boot 2 using Kotlin. By the end of the book, you will be able to build real-world applications with reactive ...
Customizing Materials Management with SAP ERP Operations by Akash Agrawal SAP PRESS, 1st edition 2009-08-30 1592292801 9781592292806 English 450
Develop next-generation web applications with ASP.NET MVC Go deep into the architecture and features of ASP.NET MVC 5, and learn how to build web applications that work well on both the desktop and ...
Embedded Android Porting Extending and Customizing Yaghmour 安卓底层平台移植指南,Karim Yaghmour 大神作品,O'REILLY 出版 很好的安卓底层移植,开发,和定制指南的英文书籍, ps:全英文版书籍,简略版,非...
Learn how to implement a DSL with Xtext and Xtend using easy-to-understand examples and best practices About This Book Leverage the latest features of Xtext and Xtend to develop a domain-specific ...
a valuable book relating to porting and customizing of embedded android.
Spring JavaMailSenderImpl does not show proper message when recipient list is empty [SPR-17540] #22072 Potential resource leak in DataSourceUtils.doGetConnection [SPR-17559] #22091 SpEL, error parsing...
Embedded Android - Porting
适用于各层次的使用microchip编程的经典教程. 高质量高清晰的电子书
Customizing Windows XP <br> By John Rizzo Publisher: Peachpit Press Pub Date: March 03, 2005 ISBN: 0-321-32124-3 Pages: 144 <br> Every new PC sold comes installed ...
Tuning and Customizing a Linux System.rar