MappingJackson2HttpMessageConverter的学习
<mvc:annotation-driven >
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.EncryptJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
继承MappingJackson2HttpMessageConverter重写read等方法
spring提供了HttpMessageConverter转换器,默认加载了MappingJackson2HttpMessageConverter(json-->Object),只需配置@RequestBody 即可使用
注册的类已经变成了RequestMappingHandlerMapping和 RequestMappingHandlerAdapter
RequestMappingHandlerAdapter(注册参数解析器@PathVariable、HttpServletRequest等)
RequestMappingHandlerMapping
1. getApplicationContext().getType(beanName)
--->class com.dimeng.p2p.common.exception.ExceptionHandler
2. isHandler(Class<?> beanType)
(AnnotationUtils.findAnnotation(beanType, Controller.class) //重载了许多方法
判断类里面是否有Controller或RequestMapping注解
3.detectHandlerMethods(final Object handler)
(1) 反射得到相应的类
Class<?> handlerType = handler.getClass()
final Class<?> userType = ClassUtils.getUserClass(handlerType);
(2)得到类中方法的集合
Set<Method> methods = HandlerMethodSelector.selectMethods()
将RequestMapping注解转换为 RequestMappingInfo实体类
-->{[/{v}/invest],methods=[POST],produces=[application/json;charset=utf-8]}
RequestMappingInfo.combine(RequestMappingInfo other)//合并类型和方法级的请求映射,返回新的请求映射信息实例
ReflectionUtils.doWithMethods(Class<?> clazz, MethodCallback mc, MethodFilter mf)
new ReflectionUtils.MethodCallback(){ //抽象方法调接口 (每个action都会这样调用)
实现dowith()方法
}
(3)遍历方法
新建HandlerMethod实体类
将mapping与HandlerMethod对应关系放在handlerMethods的map中
将pattern与mapping放在urlMap中
4.handlerMethodsInitialized(Map<T, HandlerMethod> handlerMethods)