SpringBoot響應(yīng)出現(xiàn)中文亂碼的解決方法
第一種方式
創(chuàng)建Servlet類
解決亂碼也可以直接resp.setContentType("text/html;charset=utf-8"),為了演示使用字符集過濾器類這里先不設(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ì)象,可用來測試
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)中文亂碼,因此這里需要將字符集過濾器類注冊(cè)到容器中

注冊(cè)字符集過濾器類
使用框架中的字符集過濾器類,并聲明編碼格式為utf-8,指定request和response都使用encoding的值
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterBean = new FilterRegistrationBean();
CharacterEncodingFilter filter = new CharacterEncodingFilter();
/*將文本過濾器類注冊(cè)到容器中*/
filter.setEncoding("utf-8");
filter.setForceEncoding(true);
filterBean.setFilter(filter);
filterBean.addUrlPatterns("/*");
return filterBean;
}
關(guān)閉系統(tǒng)默認(rèn)過濾器
SpringBoot中默認(rèn)配置的CharacterEncodingFilter為IS0-8859-1,設(shè)置enabled=fales目的是關(guān)閉系統(tǒng)默認(rèn)的字符集過濾器,使用上一步自定義的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過濾器,推薦使用。
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)文章
mybaties plus實(shí)體類設(shè)置typeHandler不生效的解決
這篇文章主要介紹了mybaties plus實(shí)體類設(shè)置typeHandler不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
springboot如何實(shí)現(xiàn)國際化配置
這篇文章主要介紹了springboot如何實(shí)現(xiàn)國際化配置問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
springboot加載一個(gè)properties文件轉(zhuǎn)換為map方式
這篇文章主要介紹了springboot加載一個(gè)properties文件轉(zhuǎn)換為map方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Spring boot整合mybatis實(shí)現(xiàn)過程圖解
這篇文章主要介紹了Spring boot整合mybatis實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
springboot security自定義認(rèn)證過程
這篇文章主要介紹了springboot security自定義認(rèn)證過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Spring Boot Starter 封裝的實(shí)現(xiàn)示例
Spring Boot Starter封裝了項(xiàng)目中常用的一些依賴,使得開發(fā)者可以快速啟動(dòng)和運(yùn)行項(xiàng)目,而無需手動(dòng)配置這些依賴,下面就來詳細(xì)的介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2025-09-09

