Spring Boot拦截器,拦截返回中文乱码问题。

huangapple go评论81阅读模式
英文:

Spring boot interceptor, intercept the problem of returning Chinese garbled characters

问题

Spring Boot拦截器,拦截返回的中文乱码问题。

代码结构如下:

public class demoFilter extends GenericFilterBean {

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    response.getWriter().write("您好");
    return;
  }
}

FilterConfig:

@Configuration
public class FilterConfig {

  @Bean
  public FilterRegistrationBean jwtFilter() {
    final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(new demoFilter());
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
  }
}

Controller:

@RestController
public class Demo {

  @RequestMapping(value = "demo", method = RequestMethod.GET)
  public String getStr(HttpServletRequest request, HttpServletResponse response) {
    return "demo";
  }
}

SpringApplication:

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

项目启动后,访问 http://localhost:8080/demo

实际返回:??

期望返回:您好

我在 application.properties 中添加了以下配置:

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

浏览器访问,返回:

Postman访问返回正确结果。

我想请问,如何完美解决这个问题,使浏览器返回正确的中文。

英文:

Spring boot interceptor, intercept the problem of returning Chinese garbled characters.

Code structure as shown

Spring Boot拦截器,拦截返回中文乱码问题。

code show as below

demoFilter

public class demoFilter extends GenericFilterBean {

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    response.getWriter().write("您好");
    return;

  }
}

FilterConfig

@Configuration
public class FilterConfig {

  @Bean
  public FilterRegistrationBean jwtFilter() {
    final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(new demoFilter());
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
  }

}

controller

@RestController
public class Demo {

  @RequestMapping(value = "demo", method = RequestMethod.GET)
  public String getStr(HttpServletRequest request, HttpServletResponse response) {
    return "demo";
  }
}

SpringApplication

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

Project start, visit http://localhost:8080/demo

Actual return:??

Expected return:您好

Spring Boot拦截器,拦截返回中文乱码问题。

I added the following configuration to application.properties:

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

Browser access, return:

Spring Boot拦截器,拦截返回中文乱码问题。

Postman visits and returns correct results.

Spring Boot拦截器,拦截返回中文乱码问题。

I would like to ask, how to solve this problem perfectly, so that the browser returns the correct Chinese.

答案1

得分: 0

已解决,并添加了以下代码以解决该问题:

response.setContentType("text/html;charset=UTF-8");

Spring Boot拦截器,拦截返回中文乱码问题。

Spring Boot拦截器,拦截返回中文乱码问题。

英文:

It has been solved, and the following code is added to solve the problem:

response.setContentType("text/html;charset=UTF-8");

Spring Boot拦截器,拦截返回中文乱码问题。

Spring Boot拦截器,拦截返回中文乱码问题。

huangapple
  • 本文由 发表于 2020年5月29日 14:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/62079630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定