SoFunction
Updated on 2025-03-08

spring+mybatis solution to return result Chinese garbled code through @ResponseBody

The problem occurs:

Return via @Responsebody

@ResponseBody
@RequestMapping(value ="/selectByFormId",method = )
public Map<String,Object> getClassName(String formId){
  List<String> list =(formId);
  Map<String,Object> map = new HashMap<String, Object>();
  ("data", list);
  ("status", true);
  return map;
}

result:

{"data":["Bi'er's swelling in a crazy way","Qi'an's silk","Plant","Shadowed silk","Oral supplies","Bi Xie Yan","Bi'er's Account Award","ChenNo power"electricity ",&lt;br&gt;"Snacking and panting","Hambola's key to the silhouette","Lu ㈠Guangke","Kakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakakaka","Naoyaoyaoyaoyao","�㈣South","Hanboo, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa, wa,,"Hamboo Shi Chen and Yu Ling","AssociateKing of Wild�",&lt;br&gt;"Xuanfengking�","Closed € pot $","All Bobo Bobo Bobo Bobo Bobo"],"status":true}

Solution:

Configure in

&lt;!-- solveResponseBody Garbage code problemStringHttpMessageConverterWhen this converter is converted,Parent classAbstractHttpMessageConverterofsupportedMediaTypesThe attribute default isiso-8895-1lead to --&gt;           
  &lt;mvc:annotation-driven&gt; 
    &lt;mvc:message-converters&gt; 
      &lt;bean class=""&gt; 
        &lt;property name="supportedMediaTypes" value="text/html;charset=utf-8"&gt;&lt;/property&gt; 
      &lt;/bean&gt; 
    &lt;/mvc:message-converters&gt; 
  &lt;/mvc:annotation-driven&gt; 

But after the configuration, there are new problems

error:...cvc-complex-type.2.1: element 'mvc:annotation-driven' 必须不含字符或element信息项 [Sub-level], Because the content type of this type is empty。

Later I found out that the XSD file version of springMVC is wrong, so I just need to change it to springMVC 4.0 XSD

<beans xmlns="/schema/beans"
  xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:p="/schema/p"
  xmlns:context="/schema/context"
  xmlns:mvc="/schema/mvc"
  xsi:schemaLocation="/schema/beans  
            /schema/beans/spring-beans-3.  
            /schema/context  
            /schema/context/spring-context-3.  
            /schema/mvc  
            /schema/mvc/spring-mvc-4.">

Then it's OK

{"data":["Information Request","Promotional","Management Class","Temporary Branch Class","Storage and Transportation","Assets","Information Services","Travel Management","Procurement","Personnel Attendance"&lt;br&gt;,"Financial","Customer","Visit-Top","Customer complaint","Personnel Activities","Personnel and Administration","Official Documents","Application Class","Sales and Management","Operational Business"],"status":true}
StringHttpMessageConverter This method It has many conversions,You can take a look if you have time

The solution to the above article spring+mybatis to return the results through @ResponseBody is the entire content shared by the editor. I hope it can give you a reference and I hope you support me more.