Maven+SpringMVC+Hibernate+ajax: use ajax to pass json to the front desk, there is a question mark garbled problem in Chinese

The Springmvc I use, transmits a json in the controller layer to the foreground, the background display is no problem, the Chinese is displayed normally and arrives at the foreground

Chinese becomes a question mark.

Later, it was found that because @ResponseBody was used to return json in the controller, and the implementation class of @ResponseBody in the spring source code found that its default encoding was iso-8859-1, and the encoding used in the project was utf-8, so the Chinese will appear. gibberish.

Here I use annotations to solve:


js is as follows:

$.ajax({
	url: 'sort/allSort',
	dataType: 'json',   
        type: 'post',   
	success: function(data){...}
)}



Method 1: Partial solution
@RequestMapping(value="/"allSort.do", produces = "application/json; charset=utf-8")


Method 2: Solve it globally, configure it once, and use it globally. Recommended Use

Add the following configuration to the spring core configuration file:

<mvc:annotation-driven>
     	<mvc:message-converters>
           <bean class="org.springframework.http.converter.StringHttpMessageConverter">
               <property name="supportedMediaTypes">
                   <list>
                       <value>application/json;charset=UTF-8</value>
                   </list>
               </property>
           </bean>
       	</mvc:message-converters>
     </mvc:annotation-driven>

OK, problem solved

If you still can't solve it: Complete solution


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812186&siteId=291194637