SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器( 三 )

  • 注册servlet
package com.example.web.config;import com.example.web.servlet.MyServlet;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MyWebConfig {//注册servlet@Beanpublic ServletRegistrationBean servletRegistrationBean(){ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();servletRegistrationBean.setServlet(new MyServlet());servletRegistrationBean.addUrlMappings("/test");return servletRegistrationBean;}}
  • 修改application.properties文件,让框架内置的字符集过滤器实现个性化定制
#让springboot默认的字符过滤器生效server.servlet.encoding.enabled=true#将默认的字符过滤器的编码格式设置为utf-8server.servlet.encoding.charset=utf-8#将请求和响应的编码格式都按照字符过滤器中的编码格式来进行解析server.servlet.encoding.force=true

经验总结扩展阅读