SpringBoot中SpringMVC配置

当我们需要对SpringMVC进行扩展的时候,对比如说增加一个拦截器或者新建一个参数绑定,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展

自定义参数转换器

正常情况下,前端传递来的参数都能直接被SpringMVC接收,但是也会遇到一些特殊情况,比如Date对象,当我的前端传来的一个日期时,就需要服务端自定义参数绑定,将前端的日期进行转换。自定义参数绑定也很简单,分两个步骤:


自定义参数转换器实现Converter接口,如下:

public class DateConverter implements Converter<String,Date> {
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public Date convert(String s) {
        if ("".equals(s) || s == null) {
            return null;
        }
        try {
            return simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

convert方法接收一个字符串参数,这个参数就是前端传来的日期字符串,这个字符串满足yyyy-MM-dd格式,然后通过SimpleDateFormat将这个字符串转为一个Date对象返回即可。

配置转换器

自定义WebMvcConfig继承WebMvcConfigurerAdapter,在addFormatters方法中进行配置:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateConverter());
    }
}

OK,如上两步之后,我们就可以在服务端接收一个前端传来的字符串日期并将之转为Java中的Date对象了

2.拦截器

生成拦截器,实现HandlerInterceptor接口,实现其中的方法。共有三个方法。如下:

public class MyIntecter implements HandlerInterceptor{
	/**
	 * controller执行前调用,为FALSE则controller不执行
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("执行之前调用");
		return true;
	}
	/**
	 * controller调用之后执行,且页面渲染之前调用
	 */
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("执行之后调用");
	}
	 /**
     * 页面渲染之后调用,一般用于资源清理操作
     */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}
}

将拦截器加入到Spring容器中:

@Configuration
public class SpringMVCConfig extends WebMvcConfigurerAdapter{

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new MyIntecter());
	}
}

猜你喜欢

转载自blog.csdn.net/qq_39158142/article/details/80465372