国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

springboot參數傳中文亂碼的解決方案

瀏覽:81日期:2023-03-19 18:57:19
前言

本文案例來自業務部門的一個業務場景。他們的業務場景是他們部門研發了一個微服務上下文透傳組件,其透傳原理也挺簡單的,就是通過springboot攔截器把請求參數塞進threadlocal,然后下游通過threadlocal取到值,服務之間進行feign調用時,再把threadlocal的參數塞到header頭里面。這個組件一直用得好好的,突然有一天因為傳的參數值是中文,導致亂碼。他們通過嘗試下面的各種方案,都無法解決。最后就讓我們部門排查處理。

業務部門的實現思路

他們一開始的思路方向是參數編碼不一致導致中文亂碼。于是他們就朝這個方向努力著,于是就有了如下方案

方案一:

String value = new String('我是中文亂碼'.getBytes('ISO-8859-1'),'UTF-8');

這個是常用解決字符串中文亂碼的方法之一

方案二:編寫字符編碼過濾器

@WebFilter(urlPatterns = '/*',filterName = 'CharacterEncodingFilter')public class CharacterEncodingFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; request.setCharacterEncoding('UTF-8'); response.setCharacterEncoding('UTF-8'); filterChain.doFilter(request , response); } @Override public void destroy() { }}

然后啟動類上加上@ServletComponentScan。@WebFilter是servlet3.0才有的注解。當然這個過濾器你還可以這么寫

public class CharacterEncodingFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; request.setCharacterEncoding('UTF-8'); response.setCharacterEncoding('UTF-8'); filterChain.doFilter(request , response); } @Override public void destroy() { }}

然后啟動類上加上@ServletComponentScan。@WebFilter是servlet3.0才有的注解。當然這個過濾器你還可以這么寫

public class CharacterEncodingFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; request.setCharacterEncoding('UTF-8'); response.setCharacterEncoding('UTF-8'); filterChain.doFilter(request , response); } @Override public void destroy() { }}

寫個bean配置類,如下

@Bean public FilterRegistrationBean registerAuthFilter() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new CharacterEncodingFilter(); registration.addUrlPatterns('/*'); registration.setName('CharacterEncodingFilter'); registration.setOrder(1); return registration; }方案三:在application.yml指定編碼格式為utf-8

spring: http: encoding: charset: utf-8 enabled: true force: trueserver: tomcat: uri-encoding: UTF-8方案四:寫個StringHttpMessageConverter

springboot參數傳中文亂碼的解決方案

百度來的基本上都是長這樣。不過在spring5版本WebMvcConfigurerAdapter這個類已經過時。其替代方式是實現WebMvcConfigurer接口或者繼承WebMvcConfigurationSupport。不過如果使用WebMvcConfigurationSupport,則會使springboot的mvc自動裝配失效。失效的原因是

springboot參數傳中文亂碼的解決方案

拓展一點小知識,加上@EnableWebMvc同樣也會springboot的mvc自動裝配失效。其原因是

springboot參數傳中文亂碼的解決方案

org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration這個配置類繼承WebMvcConfigurationSupport

springboot參數傳中文亂碼的解決方案

介紹那么多種方案,并沒有解決按例的問題。那問題點出在哪里?前邊案例我們提到過,在feign調用時,會把threadlocal的參數塞到header里面。真正亂碼的問題點就在這里,header是不支持中文傳輸的,如果你硬要傳輸,基本上接收方接到就是???這種看似亂碼的符號

破題關鍵

在把threadlocal的值塞到header里面時,先做下URLEncoder編碼,形如

URLEncoder.encode(“我是中文亂碼”,'UTF-8')

在接收header參數時,做下URLDecoder.解碼,形如下

URLDecoder.decode(header中待解碼的參數值, 'UTF-8')總結

方向錯了,雖然再怎么努力看似也啥沒卵用,不過至少可能會收獲其他意想不到的東西

以上就是springboot參數傳中文亂碼的解決方案的詳細內容,更多關于springboot參數傳中文亂碼的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 日韩国产在线播放 | 国产激情视频在线播放 | 又粗又爽又色男女乱淫播放男女 | 欧美videos极品 | 爽死你个放荡粗暴小淫货双女视频 | 成人毛片18女人毛片免费 | 国内久久久 | 99视频在线观看视频 | 日本九九视频 | 日本三级香港三级人妇r | 中文字幕一区二区在线视频 | 麻豆国产一区 | 欧美综合精品一区二区三区 | 国产成人精选免费视频 | 国产观看在线 | a成人在线 | 亚洲看片网| 韩国毛片 | 三级毛片免费观看 | 欧美成人免费全网站大片 | 精品久久久久中文字幕日本 | 真实国产乱子伦高清 | 真实国产普通话对白乱子子伦视频 | 女人被男人躁得好爽免费视频免费 | 99在线国产视频 | 久草在线免费播放 | 亚洲精品www | 色噜噜亚洲男人的天堂 | 国产成人aa在线观看视频 | 免费女人18毛片a级毛片视频 | 国产成人精品福利网站在线 | 中文字幕成人免费视频 | 最新国产精品视频免费看 | 国产香蕉国产精品偷在线观看 | 国产真实乱子伦精品视 | 国产最爽的乱淫视频国语对 | fc2ppv在线观看 | 一级毛片中文字幕 | 亚洲巨乳自拍在线视频 | 欧美亚洲国产成人综合在线 | 亚洲国产成人综合精品2020 |