SpringBoot響應(yīng)出現(xiàn)中文亂碼的解決方法
第一種方式
創(chuàng)建Servlet類
解決亂碼也可以直接resp.setContentType("text/html;charset=utf-8"),為了演示使用字符集過(guò)濾器類這里先不設(shè)置編碼格式,輸出流記得刷新和關(guān)閉。
package com.thugstyle.web; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<p style='color:orange'>歡迎訪問我的servlet</p>"); out.flush(); out.close(); } }
注冊(cè)Servlet
@Configuration聲明配置類,@Bean可以將方法返回值添加到Spring容器中,可由容器調(diào)用
package com.thugstyle.config; import com.thugstyle.web.MyServlet; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.CharacterEncodingFilter; @Configuration public class ConfigApplication { @Bean /*在配置類中注冊(cè)servlet*/ public ServletRegistrationBean servletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(); bean.setServlet(new MyServlet()); bean.addUrlMappings("/myServlet"); return bean; } }
啟動(dòng)容器對(duì)象
SpringApplication.run返回值為容器對(duì)象,可用來(lái)測(cè)試
package com.thugstyle; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
響應(yīng)內(nèi)容出現(xiàn)中文亂碼,因此這里需要將字符集過(guò)濾器類注冊(cè)到容器中
注冊(cè)字符集過(guò)濾器類
使用框架中的字符集過(guò)濾器類,并聲明編碼格式為utf-8,指定request和response都使用encoding的值
@Bean public FilterRegistrationBean filterRegistrationBean(){ FilterRegistrationBean filterBean = new FilterRegistrationBean(); CharacterEncodingFilter filter = new CharacterEncodingFilter(); /*將文本過(guò)濾器類注冊(cè)到容器中*/ filter.setEncoding("utf-8"); filter.setForceEncoding(true); filterBean.setFilter(filter); filterBean.addUrlPatterns("/*"); return filterBean; }
關(guān)閉系統(tǒng)默認(rèn)過(guò)濾器
SpringBoot中默認(rèn)配置的CharacterEncodingFilter為IS0-8859-1,設(shè)置enabled=fales目的是關(guān)閉系統(tǒng)默認(rèn)的字符集過(guò)濾器,使用上一步自定義的CharacterEncodingFilter編碼格式(utf-8)
server.servlet.encoding.enabled=false
再次訪問Servlet發(fā)現(xiàn)未出現(xiàn)中文亂碼
第二種方式
可以在application.properties配置文件中配置自定義編碼格式,其中編碼使能開關(guān)默認(rèn)是true可省略,
并設(shè)置請(qǐng)求與響應(yīng)都使用自定義編碼格式,這種方式不需要注冊(cè)CharacterEncodingFilter過(guò)濾器,推薦使用。
server.servlet.encoding.charset=UTF-8 server.servlet.encoding.enabled=true server.servlet.encoding.force=true
以上就是SpringBoot響應(yīng)出現(xiàn)中文亂碼的解決方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot響應(yīng)中文亂碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼
這篇文章主要介紹了Spring Security OAuth2實(shí)現(xiàn)登錄互踢的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Java編程實(shí)現(xiàn)數(shù)組轉(zhuǎn)成list及l(fā)ist轉(zhuǎn)數(shù)組的方法
這篇文章主要介紹了Java編程實(shí)現(xiàn)數(shù)組轉(zhuǎn)成list及l(fā)ist轉(zhuǎn)數(shù)組的方法,結(jié)合實(shí)例形式較為詳細(xì)的總結(jié)分析了java實(shí)現(xiàn)數(shù)組與list之間相互轉(zhuǎn)換的操作技巧,需要的朋友可以參考下2017-09-09Java String轉(zhuǎn)換時(shí)為null的解決方法
這篇文章主要介紹了Java String轉(zhuǎn)換時(shí)為null的解決方法,需要的朋友可以參考下2017-07-07關(guān)于報(bào)錯(cuò)IDEA Terminated with exit code
如果在IDEA構(gòu)建項(xiàng)目時(shí)遇到下面這樣的報(bào)錯(cuò)IDEA Terminated with exit code 1,那必然是Maven的設(shè)置參數(shù)重置了,導(dǎo)致下載錯(cuò)誤引起的,本文給大家分享兩種解決方法,需要的朋友可以參考下2022-08-08SpringBoot2.0.3打印默認(rèn)數(shù)據(jù)源為 HikariDataSource (null)問題
這篇文章主要介紹了SpringBoot2.0.3打印默認(rèn)數(shù)據(jù)源為 HikariDataSource (null)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java map 優(yōu)雅的元素遍歷方式說(shuō)明
這篇文章主要介紹了Java map 優(yōu)雅的元素遍歷方式說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10Java數(shù)據(jù)結(jié)構(gòu)之KMP算法的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)中KMP算法的原理與實(shí)現(xiàn),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-11-11Spring加載properties文件的兩種方式實(shí)例詳解
這篇文章主要介紹了Spring加載properties文件的兩種方式,需要的朋友可以參考下2018-02-02