1.springmvc 3.2以上的版本解决乱码的方法:

  第一步:在配置中加入:

 1   <mvc:annotation-driven>
 2         <mvc:message-converters register-defaults="true">
 3             <bean class="org.springframework.http.converter.StringHttpMessageConverter">
 4                 <constructor-arg value="UTF-8" />
 5             </bean>
 6             <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
 7                 <property name="objectMapper">
 8                      <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
 9                          <property name="failOnEmptyBeans" value="false" />
10                      </bean>
11                  </property>
12             </bean>
13         </mvc:message-converters>
14      </mvc:annotation-driven>

 

   第二步:需要的加入的jackson jar如下:

      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.1.0</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.1.0</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.0</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-jaxb-annotations</artifactId>
        <version>2.1.0</version>
      </dependency>    

 

2.springmvc 3.1解决中文乱码的方法:

  第一步:在配置文件中加入以下代码:

1   <mvc:annotation-driven>
2 
3     <mvc:message-converters register-defaults="true">
4 
5       <bean class="com.util.spring.UTF8StringHttpMessageConverter"/>
6 
7     </mvc:message-converters>
8 
9   </mvc:annotation-driven>

     第二步:自定义一个编码转换类:

 1     public class UTF8StringHttpMessageConverter extends 
 2         AbstractHttpMessageConverter<String> { 
 3  
 4       public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 
 5       private final List<Charset> availableCharsets; 
 6  
 7       public UTF8StringHttpMessageConverter() { 
 8           this(DEFAULT_CHARSET); 
 9       } 
10  
11       public UTF8StringHttpMessageConverter(Charset defaultCharset) { 
12           super(new MediaType("text", "plain", defaultCharset), MediaType.ALL); 
13           this.availableCharsets = new ArrayList<Charset>(Charset .availableCharsets().values()); 
14       } 
15  
16       @Override 
17       protected boolean supports(Class<?> clazz) { 
18           return String.class.equals(clazz); 
19       } 
20  
21       @Override 
22       protected String readInternal(Class<? extends String> clazz, 
23               HttpInputMessage inputMessage) throws IOException, 
24               HttpMessageNotReadableException { 
25           MediaType contentType = inputMessage.getHeaders().getContentType(); 
26           Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET; 
27           return FileCopyUtils.copyToString(new InputStreamReader(inputMessage .getBody(), charset)); 
28       } 
29  
30       @Override 
31       protected void writeInternal(String t, HttpOutputMessage outputMessage) 
32               throws IOException, HttpMessageNotWritableException { 
33           MediaType contentType = outputMessage.getHeaders().getContentType(); 
34           Charset charset = contentType.getCharSet() != null ? contentType .getCharSet() : DEFAULT_CHARSET; 
35           FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(),  charset)); 
36       } 
37  
38       protected List<Charset> getAcceptedCharsets() { 
39           return this.availableCharsets; 
40       } 
41      
42       @Override 
43       protected Long getContentLength(String s, MediaType contentType) { 
44           if (contentType != null && contentType.getCharSet() != null) { 
45               Charset charset = contentType.getCharSet(); 
46               try { 
47                   return (long) s.getBytes(charset.name()).length; 
48               } catch (UnsupportedEncodingException ex) {                 
49                   throw new InternalError(ex.getMessage()); 
50               } 
51           } else { 
52               return null; 
53           } 
54       } 
55   }

  第三步:加入需要的jackson包:

      <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.8.8</version>
      </dependency>
      <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.8</version>
      </dependency>

 

posted on 2013-06-17 15:39  小迷糊@  阅读(4689)  评论(0编辑  收藏  举报