Solve the @ResponseBody annotation to return Chinese garbled characters

When I was working on a project yesterday, I annotated it with @ResponseBody, and found that the Chinese on the returned page was garbled, and the solution process also made me very depressed! ! ! Hereby record some. There are currently the following solutions:

@RequestMapping's produces method

The first solution is to use 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 are restrictions on writing this way and can only be used on specific methods. If you need to use it globally, you need to modify the SpringMVC configuration file.

 

Using messageConverters

The second solution is to use the relevant implementation class of the HttpMessageConverter interface. Let's first look at the configuration information in the configuration file.
	<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 in 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 made me depressed yesterday was that after I configured it like this, it still did not take effect. Later, it was found that the location was wrong, and this configuration needs to be placed above <mvc:annotation-driven />. What a speechless feeling! ! !
Note: It must be placed above <mvc:annotation-driven />, otherwise it will not take effect.

使用<mvc:message-converters>

 
     还有一种方式是在SpringMVC的配置文件中的<mvc:annotation-driven>中加入<mvc:message-converters>的配置。具体配置内容如下:
    <!-- SpringMVC注解驱动 -->
    <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>

注意:始用这种配置的时候,需要去掉RequestMappingHandlerMapping、RequestMappingHandlerAdapter或者DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter的Bean配置,要不然可能会不生效。
 
另外:对于请求映射处理类返回类型可以是String也可以是Object(如果Object是JavaBean的话,SpringMVC会用Jackson来转换成json字符串).例子如下:
    @RequestMapping(value = "testPersonalValidtor.do")
    @ResponseBody
    //直接返回对象
    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;
        }
    }


 



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326770446&siteId=291194637