拦截器配置:
<!-- springMVC拦截器,可以配置多个拦截器,按照顺序执行 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- mapping如果不配置或者path配置为/**,则拦截所有的controller-->
<mvc:mapping path="/mycontroller/*">
<bean class=com.mycompany.myinteceptors.MyInteceptor></bean>
</mvc:interceptor>
</mvc:interceptors>
统一异常处理:
<!--统一异常处理-->
第一步在springmvc-servlet.xml中配置
<bean id="exceptionHandler" class="com.mycompany.exception.MyExceptionHandler" />
第二步创建MyExceptionHandler实现HandlerExceptionResolver借口:
public class MyExceptionHandler implements HandlerExceptionResolver{
public ModelAndView resolverException(
HttpServletRequest request,
HttpServletRespone respone,
Object handler,
Exception ex)
{
//code 处理异常
}
}
json转换器:
<!--jackson转换器-->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<!--Json解析-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonMessageConverter">
<property name="supportMediaTypes">
<list>
<!--返回字符窜格式json-->
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>