亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

springboot參數(shù)傳中文亂碼的解決方案

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

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

業(yè)務(wù)部門的實(shí)現(xiàn)思路

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

方案一:

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才有的注解。當(dāng)然這個過濾器你還可以這么寫

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才有的注解。當(dāng)然這個過濾器你還可以這么寫

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參數(shù)傳中文亂碼的解決方案

百度來的基本上都是長這樣。不過在spring5版本W(wǎng)ebMvcConfigurerAdapter這個類已經(jīng)過時。其替代方式是實(shí)現(xiàn)WebMvcConfigurer接口或者繼承WebMvcConfigurationSupport。不過如果使用WebMvcConfigurationSupport,則會使springboot的mvc自動裝配失效。失效的原因是

springboot參數(shù)傳中文亂碼的解決方案

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

springboot參數(shù)傳中文亂碼的解決方案

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

springboot參數(shù)傳中文亂碼的解決方案

介紹那么多種方案,并沒有解決按例的問題。那問題點(diǎn)出在哪里?前邊案例我們提到過,在feign調(diào)用時,會把threadlocal的參數(shù)塞到header里面。真正亂碼的問題點(diǎn)就在這里,header是不支持中文傳輸?shù)模绻阌惨獋鬏敚旧辖邮辗浇拥骄褪?#63;??這種看似亂碼的符號

破題關(guān)鍵

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

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

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

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

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

以上就是springboot參數(shù)傳中文亂碼的解決方案的詳細(xì)內(nèi)容,更多關(guān)于springboot參數(shù)傳中文亂碼的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久精品国产精品国产精品污 | 欧美r级限制禁片在线观看 欧美va在线高清 | 国产成人爱情动作片在线观看 | 国产偷窥自拍视频 | 97精品国产自在现线免费观看 | 一区二区在线播放福利视频 | 狠狠综合视频精品播放 | 看片国产 | 99久久精品免费看国产麻豆 | 久久精品国内一区二区三区 | 伊人精品在线 | 中文字幕一区二区精品区 | 国产一级二级三级视频 | 久久精品国产亚洲网站 | 日韩a级毛片免费观看 | 黄色录象一级片 | 亚洲综合色网站 | 中文在线第一页 | 黄色网址在线免费观看 | 亚洲国产精品一区二区久久hs | 99爱在线精品视频免费观看9 | 一级片网 | 亚洲中国日本韩国美国毛片 | 久久精品国产清自在天天线 | 大胆国模一区二区三区伊人 | 日本高清免费中文字幕不卡 | 亚洲乱码视频 | 国产激情视频在线 | 蜜桃福利视频 | 中国黄色www | 日韩国产欧美一区二区三区在线 | 国内精品51视频在线观看 | 亚洲精品乱码电影在线观看 | 国产又色又爽又黄又刺激18 | 欧美成人艳星在线播放 | 日韩国产三级 | 800玖玖爱在线观看香蕉 | 日批日韩在线观看 | 亚洲精品国产精品乱码不卞 | 成人精品一区二区三区校园激情 | 青草国产在线观看 |