Customizing HttpMessageConverters with Spring Boot and Spring MVC

本文介绍如何在Spring Boot和Spring MVC应用中定制HTTP消息转换器。通过配置自定义的MappingJackson2HttpMessageConverter,可以实现忽略实体类中不存在的属性等需求。对于纯Spring MVC应用,还需要显式注册默认的消息转换器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

### Spring Boot 中 Tomcat 的作用和功能 在 Spring Boot 项目中,Tomcat 是默认使用的嵌入式 Servlet 容器。它主要承担了处理 HTTP 请求和响应的任务,使得应用程序能够作为一个独立的服务运行而无需额外部署到外部的 Web 应用服务器上。 #### 1. 嵌入式 Web 服务器的功能 Tomcat 在 Spring Boot 中的主要职责是提供一个轻量级、高性能的 Servlet 运行环境[^3]。具体来说,它的核心功能包括以下几个方面: - **HTTP 协议支持** Tomcat 能够解析并处理客户端发送过来的标准 HTTP/HTTPS 请求,并返回相应的响应数据给客户端。 - **Servlet 和 JSP 支持** 作为标准的 Java EE Servlet 容器,Tomcat 提供对 Servlet API 的全面支持,允许开发者通过编写 Servlet 来实现复杂的业务逻辑。此外,还支持 JSP 页面渲染技术用于动态网页生成。 - **线程池管理** 内部实现了高效的线程模型来并发处理多个请求,从而提高系统的吞吐能力和响应速度。 #### 2. 自动化配置的优势 借助于 Spring Boot 的自动化机制,许多常见的设置都可以被简化甚至完全省去手动干预的过程。例如,`ServletRegistrationBean`, `FilterRegistrationBean`以及`ServletContextInitializer`这些组件会自动完成必要的初始化工作,将所有的 servlets, filters 和 listeners 正确地加载进 tomcat实例里[^2]。 #### 3. 配置灵活性 尽管 springboot 默认集成了tomcat做为其内部webserver选项之一;但是仍然给予使用者高度自由度来自定义或者替换成别的替代品比如jetty undertow等等[^1]。这意味着如果现有方案无法满足特殊场景下的性能需求或者其他考量因素,则可以很容易切换至更适合目标平台特性的解决方案上去。 ```yaml # Example of customizing embedded Tomcat properties in application.properties or yml file. server.tomcat.max-threads=200 server.port=8090 ``` 以上代码片段展示了如何调整最大线程数及更改服务监听端口等基本参数的例子[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值