springmvc 全局编码_SpringMVC-设置UTF-8编码

本文介绍了如何在SpringMVC中全局设置UTF-8编码,包括通过XML配置和注解方式改变默认的ISO-8859-1编码,以及调整读取参数时的编码。
摘要由CSDN通过智能技术生成

SpringMVC - 设置UTF-8编码

参考

全局修改输出为UTF-8编码

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

annotation方法

由方法1, 我们可以看到一个起关键作用的类org.springframework.http.converter.StringHttpMessageConverter. 打开源码可以发现:

public class StringHttpMessageConverter extends AbstractHttpMessageConverter {

// 省略 ....

public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");

private final List availableCharsets;

private boolean writeAcceptCharset = true;

public StringHttpMessageConverter(Charset defaultCharset) {

super(defaultCharset, MediaType.TEXT_PLAIN, MediaType.ALL);

this.availableCharsets = new ArrayList(Charset.availableCharsets().values());

}

// 省略 ....

}

可以看到, 默认编码已经被设定为了Charset.forName("ISO-8859-1"). 所以我们需要想办法替换掉这个默认编码. 方法1是通过xml加载了参数为UTF-8的StringHttpMessageConverter.

回到使用注解配置的Java代码中:

因为配置SpringMVC的类, 需要继承于WebMvcConfigurerAdapter, 我们可以看看里面有没有相关方法可用.

public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer{...}

// 所以继续看WebMvcConfigurer

public interface WebMvcConfigurer {

/**

* Configure the {@link HttpMessageConverter}s to use for reading or writing

* to the body of the request or response. If no converters are added, a

* default list of converters is registered.

*

Note that adding converters to the list, turns off

* default converter registration. To simply add a converter without impacting

* default registration, consider using the method

* {@link #extendMessageConverters(java.util.List)} instead.

* @param converters initially an empty list of converters

*/

void configureMessageConverters(List> converters);

/**

* A hook for extending or modifying the list of converters after it has been

* configured. This may be useful for example to allow default converters to

* be registered and then insert a custom converter through this method.

* @param converters the list of configured converters to extend.

* @since 4.1.3

*/

void extendMessageConverters(List> converters);

}

可以看到configureMessageConverters和extendMessageConverters两个方法. 区别是前者会覆盖掉默认的Converter, 而后者是扩展. 所以我们只需要在我们继承于WebMvcConfigurerAdapter这个类的实现中覆盖掉configureMessageConverters方法即可.

实现如下:

@Configuration

@EnableWebMvc

@ComponentScan("me.xiaofud.spring101.spittr.web")

public class WebConfig extends WebMvcConfigurerAdapter {

@Override

public void configureMessageConverters(List> converters) {

StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));

stringHttpMessageConverter.setWriteAcceptCharset(false);

converters.add(stringHttpMessageConverter);

}

测试:

curl -I -X POST "http://localhost:8080/app/post/add"

HTTP/1.1 200 OK

Server: Apache-Coyote/1.1

Content-Type: text/plain;charset=UTF-8

Content-Length: 1338

Date: Sun, 12 Nov 2017 14:17:49 GMT

修改读取参数时候的编码

在web.xml中:

添加一个filter, 注册org.springframework.web.filter.CharacterEncodingFilter.

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值