Spring MVC后台返回JSON数据中文乱码问题解决

来源:互联网 发布:易知国学门 编辑:程序博客网 时间:2024/04/28 12:41

spring3.2 


@RequestMapping(value="/findTypeInfo",produces="text/html;charset=UTF-8")

      @ResponseBody
      public String findTypeInfo(String productId) throws BaseException{
         String result="";
         try {
         result = productService.findTypeInfoByProductId(productId);
        }catch(Exception e){
            throw new BaseException("SerachTypeInfoException", e, SaleErrorCode.SALE_ERROR_SERACHTYPEINFO);
       }
        return result;

     }


只要加上produces="text/html;charset=UTF-8"即可



spring3.0

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->   
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
    <property name="messageConverters">  
             <list>  
                 <bean class = "org.springframework.http.converter.StringHttpMessageConverter">  
                    <property name = "supportedMediaTypes">
                          <list>
                              <value>text/html;charset=UTF-8</value>  
                         </list>  
                    </property>  
                 </bean>  
             </list>  
       </property>
    </bean>