SpringBoot響應出現(xiàn)中文亂碼的解決方法
第一種方式
創(chuàng)建Servlet類
解決亂碼也可以直接resp.setContentType("text/html;charset=utf-8"),為了演示使用字符集過濾器類這里先不設置編碼格式,輸出流記得刷新和關閉。
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();
}
}
注冊Servlet
@Configuration聲明配置類,@Bean可以將方法返回值添加到Spring容器中,可由容器調用
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 /*在配置類中注冊servlet*/
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new MyServlet());
bean.addUrlMappings("/myServlet");
return bean;
}
}
啟動容器對象
SpringApplication.run返回值為容器對象,可用來測試
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);
}
}
響應內容出現(xiàn)中文亂碼,因此這里需要將字符集過濾器類注冊到容器中

注冊字符集過濾器類
使用框架中的字符集過濾器類,并聲明編碼格式為utf-8,指定request和response都使用encoding的值
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterBean = new FilterRegistrationBean();
CharacterEncodingFilter filter = new CharacterEncodingFilter();
/*將文本過濾器類注冊到容器中*/
filter.setEncoding("utf-8");
filter.setForceEncoding(true);
filterBean.setFilter(filter);
filterBean.addUrlPatterns("/*");
return filterBean;
}
關閉系統(tǒng)默認過濾器
SpringBoot中默認配置的CharacterEncodingFilter為IS0-8859-1,設置enabled=fales目的是關閉系統(tǒng)默認的字符集過濾器,使用上一步自定義的CharacterEncodingFilter編碼格式(utf-8)
server.servlet.encoding.enabled=false

再次訪問Servlet發(fā)現(xiàn)未出現(xiàn)中文亂碼

第二種方式
可以在application.properties配置文件中配置自定義編碼格式,其中編碼使能開關默認是true可省略,
并設置請求與響應都使用自定義編碼格式,這種方式不需要注冊CharacterEncodingFilter過濾器,推薦使用。
server.servlet.encoding.charset=UTF-8 server.servlet.encoding.enabled=true server.servlet.encoding.force=true

以上就是SpringBoot響應出現(xiàn)中文亂碼的解決方法的詳細內容,更多關于SpringBoot響應中文亂碼的資料請關注腳本之家其它相關文章!
相關文章
Spring Security OAuth2 實現(xiàn)登錄互踢的示例代碼
這篇文章主要介紹了Spring Security OAuth2實現(xiàn)登錄互踢的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Java編程實現(xiàn)數(shù)組轉成list及l(fā)ist轉數(shù)組的方法
這篇文章主要介紹了Java編程實現(xiàn)數(shù)組轉成list及l(fā)ist轉數(shù)組的方法,結合實例形式較為詳細的總結分析了java實現(xiàn)數(shù)組與list之間相互轉換的操作技巧,需要的朋友可以參考下2017-09-09
關于報錯IDEA Terminated with exit code
如果在IDEA構建項目時遇到下面這樣的報錯IDEA Terminated with exit code 1,那必然是Maven的設置參數(shù)重置了,導致下載錯誤引起的,本文給大家分享兩種解決方法,需要的朋友可以參考下2022-08-08
SpringBoot2.0.3打印默認數(shù)據(jù)源為 HikariDataSource (null)問題
這篇文章主要介紹了SpringBoot2.0.3打印默認數(shù)據(jù)源為 HikariDataSource (null)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java數(shù)據(jù)結構之KMP算法的實現(xiàn)
這篇文章主要為大家詳細介紹了Java數(shù)據(jù)結構中KMP算法的原理與實現(xiàn),文中的示例代碼講解詳細,對我們學習Java有一定的幫助,需要的可以參考一下2022-11-11

