欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot響應(yīng)出現(xiàn)中文亂碼的解決方法

 更新時(shí)間:2024年02月03日 10:22:48   作者:K_arot  
這篇文章主要介紹了SpringBoot響應(yīng)出現(xiàn)中文亂碼的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起來(lái)學(xué)習(xí)吧

第一種方式

創(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)文章

最新評(píng)論