实际开发中很多地方会有类似拦截器的应用,那么springboot中如何自定义一个拦截器呢?
在xml时代,拦截器都是在xml中配置的,现在我在springboot项目中自定义一个拦截器。
第一步:
新建一个springboot项目。具体步骤同昨天那一篇微博里面提到的新建SpringBoot项目步骤一致。
新建完成后创建包com.itcast.springboot.selfdefinitioninterceptor,在包里面新建入口类InterceptorSpringbootApp
,入口类中有main方法,调用启动程序的run方法,同时需要写一个测试方法hello(),以便后续访问服务调用,具体代码如下:
package com.itcast.springboot.selfdefinitioninterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication
public class InterceptorSpringbootApp {
@RequestMapping("/sayHello")
@ResponseBody
public String hello(){
return "hello world springboot!自定义拦截器!";
}
public static void main(String[] args) {
SpringApplication.run(InterceptorSpringbootApp.class, args);
}
}
入口类写完之后,在同一个包下新建一个拦截器配置类MySrpingMVCConfig.java,这个拦截器类需要继承WebMvcConfigurerAdapter适配器类,重写addInterceptors()方法:
@Override
public void addInterceptors(InterceptorRegistry registry) {
}
,在方法体中实例化拦截器处理器的匿名内部类,这个是WebMvcConfigurerAdapter中addInterceptors()封装了的,具体代码如下:
package com.itcast.springboot.selfdefinitioninterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration//申明这是一个配置
public class MySrpingMVCConfig extends WebMvcConfigurerAdapter {
//自定义一个拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("-------这个是我自定义的拦截器-------我可以为所欲为----");
return true;
}
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
};
};
registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
}
}
写完这个拦截器的demo后我们实际操作一个消息转化器的拦截器的实际运用:关于中文乱码的问题:我本机默认的编码格式是utf-8,下面新建一个包com.itcast.springboot.messageconvert,包下新建入口类,同刚才的上面的那个一致即可,在入口类中加上一个消息转化器的方法来创建StringHttpMessageConverter对象,我本地是默认的utf-8,那么在消息转化过程中Charset.forName("UTF-8")设置为utf-8不会出现乱码,设置为Charset.forName("ISO-8859-1")则会出现中文乱码,其实关于这个消息转化器的StringHttpMessageConverter对象,底层是使用到一个条件注解
意思就是说当没有这个消息转化器对象时才会调用这个方法,有的话就会使用现有的消息转化器对象
package com.itcast.springboot.messageconvert;
import java.nio.charset.Charset;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Configuration
@Controller
@SpringBootApplication
public class InterceptorSpringbootApp {
@RequestMapping("/sayHello")
@ResponseBody
public String hello() {
return "hello world springboot!自定义拦截器1111!";
}
public static void main(String[] args) {
SpringApplication.run(InterceptorSpringbootApp.class, args);
}
@Bean
public StringHttpMessageConverter stringHttpMessageConverter() {
// StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("ISO-8859-1"));
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
}
运行结果:
改为ISO-8859-1之后就会出现
拦截器的作用就这样可以解决中文乱码的问题。尽情期待下次分享吧。