标签:val scan ns-3 coding 使用 ext 默认 defaults pos
一下这些都是直接从园友直接搬过来的
xml
配置文件的方法.xml
配置的, 关于输入的编码的问题.<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!-- utf-8编码 --> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans> 作者:xiaofudeng 链接:https://www.jianshu.com/p/f6f28576a564 來源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
org.springframework.http.converter.StringHttpMessageConverter
. 打开源码可以发现:
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { // 省略 .... public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1"); private final List<Charset> availableCharsets; private boolean writeAcceptCharset = true; public StringHttpMessageConverter(Charset defaultCharset) { super(defaultCharset, MediaType.TEXT_PLAIN, MediaType.ALL); this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values()); } // 省略 .... } 作者:xiaofudeng 链接:https://www.jianshu.com/p/f6f28576a564 來源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Charset.forName("ISO-8859-1")
. 所以我们需要想办法替换掉这个默认编码. 方法1是通过xml
加载了参数为UTF-8
的StringHttpMessageConverter
.WebMvcConfigurerAdapter
, 我们可以看看里面有没有相关方法可用.@Configuration @EnableWebMvc @ComponentScan("me.xiaofud.spring101.spittr.web") public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8")); stringHttpMessageConverter.setWriteAcceptCharset(false); converters.add(stringHttpMessageConverter); } 作者:xiaofudeng 链接:https://www.jianshu.com/p/f6f28576a564 來源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
测试:
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
.
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
标签:val scan ns-3 coding 使用 ext 默认 defaults pos
原文地址:https://www.cnblogs.com/cheng88/p/10064428.html