SpringMVC fixed @ResponseBody annotation returning Chinese gibber

  • 2020-06-23 00:17:32
  • OfStack

Yesterday when I was working on the project, I annotated @ResponseBody and found that the Chinese code on the return page was a mess. The solution process also depressed me!! I hereby record some. The following solutions are available:

produces method for @RequestMapping

The first solution is the produces method annotated with @RequestMapping. It is written as follows:


@RequestMapping(value = "testPersonalValidtor.do",produces = "application/json;charset=utf-8")  

Just add this annotation to the method. However, there is a limit to this writing and it can only be used in certain methods. You need to modify the SpringMVC configuration file if you want to use it globally.

Using messageConverters

The second solution is to use the associated implementation classes of the HttpMessageConverter interface. Let's look at the configuration information in the configuration file first.


<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > 
 <property name="messageConverters"> 
  <list> 
   <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
   <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
    <property name="supportedMediaTypes"> 
     <list> 
      <value>text/plain;charset=utf-8</value> 
      <value>text/html;charset=UTF-8</value> 
     </list> 
    </property> 
   </bean> 
  </list> 
 </property> 
</bean> 

And you need to configure Jackjson dependencies on Maven dependencies.


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

What depressed me yesterday was that it still didn't work after I configured it this way. Later I found out that the location was wrong, so I need to put this configuration in < mvc:annotation-driven / > Of the above. What a speechless feeling!!

Note: 1 must be put in < mvc:annotation-driven / > The above, otherwise will not take effect.

use < mvc:message-converters >

Another way is in the SpringMVC configuration file < mvc:annotation-driven > add < mvc:message-converters > The configuration. The specific configuration contents are as follows:


<!-- SpringMVC Annotation driven  --> 
<mvc:annotation-driven> 
 <mvc:message-converters> 
  <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> 
  <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
   <property name="supportedMediaTypes"> 
    <list> 
     <value>text/plain;charset=utf-8</value> 
     <value>text/html;charset=UTF-8</value> 
    </list> 
   </property> 
  </bean> 
 </mvc:message-converters> 
</mvc:annotation-driven> 

Note: RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or DefaultAnnotationHandlerMapping, AnnotationMethodHandlerAdapter's Bean configuration needs to be removed to start using this configuration, otherwise it may not take effect.

Also: The return type of the request mapping handler class can be String or Object (if Object is JavaBean, SpringMVC will be converted to json string with Jackson). Examples are as follows:


@RequestMapping(value = "testPersonalValidtor.do") 
@ResponseBody 
// Direct return object  
public Object testPersonalValidtor(@Valid PersonScope personScope, BindingResult bindingResult){ 
 if(bindingResult.hasErrors()){ 
  StringBuffer sb = new StringBuffer(); 
  for(ObjectError objectError : bindingResult.getAllErrors()){ 
   sb.append(((FieldError)objectError).getField() +" : ").append(objectError.getDefaultMessage()); 
  } 
  return sb.toString(); 
 }else{ 
  return personScope; 
 } 
} 

Related articles: