SpringMVC返回字符串中文乱码

更新时间:2023-07-10 03:22:15 阅读: 评论:0

SpringMVC返回字符串中⽂乱码
@ResponBody
@RequestMapping(value = "/save")
public String callSomeone() {
return "保存成功";
}
显⽰乱码
设置了CharacterEncodingFilter为UTF-8,
⽅法⼀:
指定RequestMapping的produces属性
moskau@RequestMapping(value = "/addOrder",produces = "text/html;chart=utf-8")
public String addOrder(){
Order order = new Order(100,"连⾐裙",8);
orderService.addOrder(order);
return "增加订单成功";
}
⽅法⼆:
SpringMVC返回字符串的编码与StringHttpMessageConverter的DefaultChart属性有关
修改StringHttpMessageConverter的DefaultChart属性
超速绯闻插曲
新建⼀个⼯具类 AppUtil实现ApplicationContextAware接⼝,并监听ContextRefreshedEvent事件
@Component
public class AppUtil implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> {
2015六级private ApplicationContext app;
@Override
genitalpublic void tApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.app = applicationContext;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
try {
RequestMappingHandlerAdapter requestMappingHandlerAdapter = Bean(RequestMappingHandlerAdapter.class);
if(requestMappingHandlerAdapter!=null) {
regalList<HttpMessageConverter<?>> messageConverters = MessageConverters();
magistaif(messageConverters!=null) {
protester
// 获取bean容器中的StringHttpMessageConverter,并修改DefaultChart属性
for(HttpMessageConverter item : messageConverters) {
if(item instanceof StringHttpMessageConverter) {如何梳头
((StringHttpMessageConverter) item).tDefaultChart(StandardCharts.UTF_8);
盘弄
}
}
}
}
}catch (BeansException e) {
paran
}
}
}
<bean id="mappingJackson2HttpMessageConverter" class="org.verter.json.MappingJackson2HttpMessageConverter">        <property name="supportedMediaTypes">
<list>
<value>text/html;chart=UTF-8</value>
</list>
</property>
</bean>
设置返回json数据的编码为UTF-8。

本文发布于:2023-07-10 03:22:15,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/172656.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:属性   返回   字符串   编码   超速   订单
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图