From dbc13986fa2a82d265eecc20dc77c5720d936989 Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Tue, 10 Feb 2015 23:41:27 +0800 Subject: [PATCH] Update 26.1.2. HttpMessageConverters.md --- .../26.1.2. HttpMessageConverters.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/IV. Spring Boot features/26.1.2. HttpMessageConverters.md b/IV. Spring Boot features/26.1.2. HttpMessageConverters.md index e69de29..68b076d 100644 --- a/IV. Spring Boot features/26.1.2. HttpMessageConverters.md +++ b/IV. Spring Boot features/26.1.2. HttpMessageConverters.md @@ -0,0 +1,22 @@ +### 26.1.2. HttpMessageConverters + +Spring MVC使用HttpMessageConverter接口转换HTTP请求和响应。合理的缺省值被包含的恰到好处(out of the box),例如对象可以自动转换为JSON(使用Jackson库)或XML(如果Jackson XML扩展可用则使用它,否则使用JAXB)。字符串默认使用UTF-8编码。 + +如果需要添加或自定义转换器,你可以使用Spring Boot的HttpMessageConverters类: +```java +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.context.annotation.*; +import org.springframework.http.converter.*; + +@Configuration +public class MyConfiguration { + + @Bean + public HttpMessageConverters customConverters() { + HttpMessageConverter additional = ... + HttpMessageConverter another = ... + return new HttpMessageConverters(additional, another); + } +} +``` +任何在上下文中出现的HttpMessageConverter bean将会添加到converters列表,你可以通过这种方式覆盖默认的转换器(converters)。