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是一个开源Java基础框架,由Pivotal团队提供的,它用来简化Spring应用的初始搭建以及开发过程。Spring Boot提供了大量默认配置来简化项目配置,并且易于创建独立的、生产级别的基于Spring框架的应用。 ...
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 ...
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...
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 ...
在接下来的内容中,我将详细描述标题《Spring Cloud Finchley.SR1-Spring Cloud 手册-Spring Cloud 文档》与《Spring Cloud 2.x手册-Spring Cloud 2.x 文档》以及标签“springCloud spring 微服务”中涉及的知识点。...
第八章:自定义和扩展Spring Security(Customizing and Extending Spring Security) - 拦截器和过滤器:说明如何在Spring Security中自定义拦截器和过滤器来满足特定的安全需求。 - 安全组件扩展:介绍Spring ...
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....
Spring Cloud是一个基于Spring Boot实现的云原生开发工具集,它提供了一套完整的分布式系统解决方案,用于快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次...
### Spring5.0中文开发手册知识点详述 #### 一、Spring框架概览 ##### 1.1 Spring入门 Spring框架作为一个轻量级的应用程序框架,它的设计目标是为了简化企业级应用的开发过程。Spring提供了丰富的功能来帮助...
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 Bootstrap 3 with LESS" 的主题中,我们将深入探讨如何利用 LESS 来定制和优化 Bootstrap 3 的样式。 首先,了解 Bootstrap 3 的结构至关重要。这个框架的核心在于其 Grid 系统、响应式布局、可...
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 ...
综上所述,《嵌入式Android - Porting, Extending and Customizing》这本书不仅深入浅出地讲解了如何将Android系统移植到各种嵌入式设备上,还提供了大量的实践案例和实用技巧,对于希望在这个领域有所作为的专业...
根据提供的信息,我们可以深入探讨Spring 4框架的相关知识点,包括其新特性、核心技术和关键概念。下面将逐一解析这些内容。 ### Spring Framework 总览 #### 开始与介绍 Spring Framework是一个开源的应用程序...
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 ...
### 嵌入式Android移植、扩展与定制 #### 知识点概览 1. **嵌入式Android概述** 2. **移植Android系统至不同硬件平台** 3. **定制化Android系统的方法** 4. **扩展Android功能的技术** 5. **Android构建系统的深入...