英文:
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
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:您好
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:
Postman visits and returns correct results.
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论