转载

spring boot 2.X 一 自动装配(后补)

  • 组件自动装配: web mvc、web flux、jdbc等
  • 嵌入式web容器:tomcat、jetty以及undertow等
  • 生产准备特性:指标、健康检查、外部配置等

组件的自动装配

  • 激活:@EnableAutoConfiguration
  • 配置:/META-INF/spring.factories
  • 实现:XXXAutoConfiguration

传统web

支持jsp

添加依赖

<!-- 支持jsp -->
<dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <version>9.0.0.M15</version>
</dependency>
<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
</dependency>
<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
</dependency>
复制代码

配置文件

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
复制代码

直接编写cotroller及路由,便可以访问对应的jsp页面

fastjson解析返回数据

@Configuration
public class FastJsonConfigInfo implements WebMvcConfigurer{

	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		converter.setFastJsonConfig(fastJsonConfig);
		converters.add(converter);
	}
}

复制代码

拦截器

  • 自定义拦截器
public class CustomerInterceptor implements HandlerInterceptor {

	private final Logger logger = LoggerFactory.getLogger(CustomerInterceptor.class);

	/**
	 * 针对那些情况做拦截:
	 *  false则不处理,直接返回。
	 *  true 交给对应的handler处理
	 * @param request
	 * @param response
	 * @param handler
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {
		String path = request.getServletPath();
		logger.info("request of path is {}",path);
		if("/indexJson".equals(path)){
			return true;
		}
		return false;
	}

	/**
	 * handler 执行之前处理,数据校验等
	 * @param request
	 * @param response
	 * @param handler
	 * @param modelAndView
	 * @throws Exception
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
		@Nullable ModelAndView modelAndView) throws Exception {
		Map<String,String[]> params = request.getParameterMap();
		logger.info("request params info is {}",JSONObject.toJSONString(params));
		logger.info("{}",handler.getClass().getName());
	}

	/**
	 * hanler 处理完成之后
	 * @param request
	 * @param response
	 * @param handler
	 * @param ex
	 * @throws Exception
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
		@Nullable Exception ex) throws Exception {
		logger.error("the request is exception,the exception info is",ex);
	}
}
复制代码
  • 将自定义的拦截器注册到容器中
@Configuration
public class FastJsonConfigInfo implements WebMvcConfigurer{

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		converter.setFastJsonConfig(fastJsonConfig);
		converters.add(converter);
	}
    /**
     将自定义的拦截器注册到容器中
    ***/
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new CustomerInterceptor()).addPathPatterns("/**");
	}
}

复制代码
原文  https://juejin.im/post/5cb3dfaee51d456e6e389212
正文到此结束
Loading...