When spring4 uses @ResponseBody to return Chinese, it is found that the client is garbled

When using spring4 to use @ResponseBody to return Chinese, I found that the client is garbled. Strange, the filter has been configured in web.xml

The configuration is as follows:

 <filter>

        <description>Character set filter</description>

        <filter-name>encodingFilter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>

            <description>Character set encoding</description>

            <param-name>encoding</param-name>

            <param-value>UTF-8</param-value>

            

            <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>

 

Later, after checking the information, it was found that @RequestMapping added produces configuration 

The configuration is as follows

@RequestMapping(value="user/reg.do",method =RequestMethod.POST,produces={"application/json;charset=UTF-8"})

The returned Chinese is not garbled.

 

charset=UTF-8: Set the character set to utf-8

@RequestMapping(value = "/user/reg.do", produces = "application/xml"): Indicates that the function processing method will produce data in xml format, at this time, it is matched according to the Accept in the request header, such as the request header " Accept:application/xml" to match.

 

This method can better indicate your purpose than using "headers = "Accept=application/json"" of @RequestMapping.

 

When you have the following Accept header:

①Accept:text/html,application/xml,application/json

Produces will be matched in the following order ①text/html ②application/xml ③application/json

②Accept:application/xml;q=0.5,application/json;q=0.9,text/html

Produces will be matched in the following order ①text/html ②application/json ③application/xml

The q parameter is the quality factor of the media type, the larger the higher the priority (from 0 to 1)

③Accept:*/*,text/*,text/html

Produces will be matched in the following order ①text/html ②text/* ③*/*

 

That is, the matching rule is: the most explicit first match.

 

 

2. When using the response to return the json string, the Chinese is garbled again.

Code:

result = mapper.writeValueAsString(resReslt);

pt = response.getWriter();

 

pt.write(result);

 

After modifying the code:

 

response.setHeader("Content-type", "text/html;charset=UTF-8");  

//The meaning of this sentence is to tell the servlet to use UTF-8 transcoding instead of the default ISO8859  

response.setCharacterEncoding("UTF-8");  

pt = response.getWriter();

 

pt.write(result);

 

After setting the message header and character set, the Chinese characters are not garbled.

 

 

 

 

Guess you like

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