项目中使用到前端传json数据给后台用@RequestBody进行接收,并解析为实体类。
查看接收到的json数据时发现中文编程乱码:
在applicationContext.xml 中将
<mvc:annotation-driven />
修改为:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prettyPrint" value="true"/>
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
重启项目即可解决问题。
备注:
在解决过程中进行搜索解决办法时发现了其他两种办法,但是并未解决问题
1. 修改@PostMapping 的值,增加products
@PostMapping(value = "/insertRisk",produces = "application/json;charset=UTF-8")
2. 修改ajax传值的heads,增加
contentType:application/json;charset=UTF-8
参考:
<mvc:annotation-driven /> 注解驱动:
https://blog.csdn.net/vicroad2014/article/details/82527863
{{ cmt.username }}
{{ cmt.content }}
{{ cmt.commentDate | formatDate('YYYY.MM.DD hh:mm') }}