您的位置:首页 > 编程语言 > Java开发

springmvc返回数据中文乱码

2014-08-28 10:02 393 查看
今天用springmvc作为接口产生了中文乱码,因为返回的是string 就不能用以前的更改responseBody了 。

在spring-mvc.xml文件中:

<mvc:annotation-driven/>更改为

<mvc:annotation-driven>

<mvc:message-converters>

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

解决问题。

@RequestMapping(value="/getWorks.html" ,method=RequestMethod.POST)

public @ResponseBody String getWork(@RequestParam(value = "xml") String xml,HttpServletResponse response) throws UnsupportedEncodingException {

System.out.println("xml:"+xml);

ViewYyRecord record=null;

try {

record=RecordXmlUtil.getRecords(xml);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//WorkService workService=(WorkService) SpringContextUtil.getBean("workService");

String ret=workService.getWork(record);

System.out.println("最后结果"+ret);

return ret;

}

调用该接口,返回数据中文未出现乱码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: