spring cloud中返回数据编码问题
来源:互联网 发布:服务器网络架构 编辑:程序博客网 时间:2024/06/06 09:32
spring cloud集成了spring mvc,默认返回的格式都是json,utf-8。 如果在项目中发现返回的json失败,或者编码有问题,那需要检查一下是否做了以下配置:
@Configurationpublic class CustomMVCConfiguration extends WebMvcConfigurerAdapter{@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {}@Overridepublic void configureContentNegotiation(ContentNegotiationConfigurer configurer) {}}
如果需要自定义配置,需要对下面配置进行编码和数据的转换:
@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) { /* using the StringHttpMessageConverter to handle with simple String message. */StringHttpMessageConverter stringConverter= new StringHttpMessageConverter();stringConverter.setDefaultCharset(Charset.forName("UTF-8"));converters.add(stringConverter); /* using the FastJsonHttpMessageConverter to handle these below. 1. text/html;charset=UTF-8 a page(htm/html/jsp etc.) 2. application/json;charset=utf-8 json data type response 3. text/plain;charset=UTF-8 a text or string etc. 4. application/x-www-form-urlencoded;charset=utf-8 standard encoding type. convert form data to a key-value. ... */FastJsonHttpMessageConverter4 fastJsonConverter = new FastJsonHttpMessageConverter4();FastJsonConfig fastJsonConfig = new FastJsonConfig();fastJsonConfig.setCharset(Charset.forName("UTF-8"));fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);fastJsonConverter.setFastJsonConfig(fastJsonConfig);List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();MediaType text_plain = new MediaType(MediaType.TEXT_PLAIN, Charset.forName("UTF-8"));MediaType text_html = new MediaType(MediaType.TEXT_HTML, Charset.forName("UTF-8"));MediaType x_www_form_urlencoded_utf8 = new MediaType(MediaType.APPLICATION_FORM_URLENCODED, Charset.forName("UTF-8"));supportedMediaTypes.add(text_html);supportedMediaTypes.add(text_plain);supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);supportedMediaTypes.add(x_www_form_urlencoded_utf8);fastJsonConverter.setSupportedMediaTypes(supportedMediaTypes);converters.add(fastJsonConverter);super.configureMessageConverters(converters);}@Overridepublic void configureContentNegotiation(ContentNegotiationConfigurer configurer) {configurer.favorPathExtension(false);}
阅读全文
0 0
- spring cloud中返回数据编码问题
- requests返回数据编码问题
- Spring cloud中Hystrix仪表盘中各项数据的含义
- spring cloud-Feign使用中遇到的问题总结
- Spring MVC中ajax返回json数据
- Spring中使用FastJson返回json数据
- Spring mvc 注解@ResponseBody 返回内容编码问题
- spring cloud 问题描述1
- spring-cloud跨域问题
- spring cloud遇到的问题
- [K/3Cloud]DBServiceHelper.ExecuteDataSet(this.Context, sql)) 返回数据问题
- spring mvc中设置返回的http status自定义编码
- PHP返回JSON数据及中文编码问题的解决方案
- Spring Cloud (16) | Spring Cloud微服务重构问题总结
- Fiddler中返回数据乱码问题
- MySQL编码、Spring配置中编码、Struts中文问题
- 关于python中pymysql数据编码问题
- mysql中insert数据编码错误问题
- jQuery 基本选择器
- Python之路【第七篇续】:I/O多路复用
- Java去除字符串中的空格、回车、换行符、制表符
- 45. Jump Game II
- 揭开软件行业毒瘤思杰马克丁的狰狞面目
- spring cloud中返回数据编码问题
- Python之路【第八篇】:堡垒机实例以及数据库操作
- RBAC 权限控制设计
- oracle相关设置
- H5六人牛牛源码合集代码分享
- sort 在字符串中的用法(洛谷di12题)
- 数据离散化的意义
- Import cv2 ImportError:DLL load failed:找不到指定模块 解决方法
- 利用LSTM预测股票日最高价