开发工具:
文件大小: 1mb
下载次数: 0
上传时间: 2019-08-10
详细说明:NULL
博文链接:https://zzc1684.iteye.com/blog/2210510return map
直接访问地址:
P Alelocalhost: 8888/springMVCDemo/employee/ xmlorJson
圆访问最多 gEtting Started
t list": I id: 1, name": format", age": 22, dept: id": 1, name": research and develop F
我们看到,短短几行配置。使用 ResponseBody注解之后, Controller返回的对象自动被转换成对应的json数据,在这里不得不感
叹 Springmvc的强大
我们好像也没看到具体的配置,唯一看到的就是κ dispatcher.xm中的一句配置:。其实就是这个配置,
导致了jaA对象自动转换成json对象的现象
那么 spring到底是如何实现java对象到json对象的自动转换的呢?为什么转换成∫json数据,如果想转换成xml数据,那该怎么
办
源码分析
本文使用的 spring版本是4.0.2。
在讲解这句配置
这句代码在 spring中的解析类是:
1b l1c interface BeanDefinitionParser t
Choose Implementation of Bean DefinitionParser (50 found)
C Abstract Bean Definition Parser (org. springfram ework bean s factory xmD)
Maven, org. springfram ework spring-be
w Clink BeanDefinition ed o Abstractin diLocatingBean DefinitionParser (org. springfram ework ejb. config
Maven: org. springframeworkspring-con
w ialink org. springframewd a AbstractListenerCont ainerParser (org springfram eworkims config)
Maven: org springfram ework:sp
k embedded in the supplied C Abstract Property LoadingBean Definition Parser (org springfram eworkcontext. config
Maven: org. springframeworkspring-cont
k spsImpLementations must a Abstract simple Bean D efinitionParser (org springfram ework beans f actory xml)
Maven org. springfram ework spring-bi
k from the parse if they e AbstractSingleBean Definition Parser (org springfram ework beans factory xml)
Maven: org springframework spring-be
k tacode null if they
C)Annotation Config BeanD Parser forg. springfram Ework context. annotation
Maven: org springtrameworkspring-conte
k param element the elend O Annot ationDrivenBeanDefinitionParser (org. springfram ework scheduling. config)
Maven: org. springframeworkspring-cont
w param parserContext thy C Annot ation Driven BeanDefinition Parser (org springfram ework transaction. config)
Maven, org. springfram eworks
provides access to a
C Annot ation Driven Bean Definition Parser (org springfram eworkweb, servlet config)
Maven: org. springfram ework spring-wabm
ereturn the primary al a AnnotationDriven Cache Bean DefinitionParser org springframework cache. config)
Maven org springframeworkspring-conte
springmvc Torg m orb ay jetty jetty-ml O AspectAutoProxy Bean Definition Parser (org. springframework aop.config
Maven: org springfram eworksprin
在 Annotationdriven beanDefinitionParser源码的152行 parse方法中
分别实例化了 RequestMappinghandler mapping, ConfigurableWebBindingInitializer, RequestMappinghandlerAdapter等诸多类。
其中 RequestMappingHandlerMapping和 RequestMappingHandlerAdapter这两个类比较重要。
Reques tMappinghandlermapping处理请求映射的,处理 Reques tApping跟请求地址之间的关系
RequestMappinglandlerAdapter是请求处理的适配器,也就是请求之后处理具休逻辑的执行,关系到哪个类的哪个方法以及转换器等
工作,这个类是我们讲的重点,其中它的属性 message Converters是本文要讲的重点。
ManagedList> message Converters getMessage Conve rters (element, source, parserContext
ManagedList> argument Resolvers getArgumen tRe solvers (e lement, parserContext)
ManagedList> returnVa lueHandlers getReturnVa lueHand lers (element. parserContext)
String asyncTimeout= getAsyncTimeout (element
Runtime Bean Reference asyncExecutor getAsync Executor(element)
ManagedList> callableInterceptors getcallableIntercep tors (element, source, parserContext)
ManagedList> deferredResultInterceptors getDeferredResultInterceptors (element, source, parsercontext):
RootBeanDefinition hand lerAdapterDef= new RootBeanDefinition(RequestMappingHandlerAdapter class
handlerAdapterDef, set Source(source)
handlerAdapterDef. setRo le(Bean Definition, ROLE INFRASTRUCTURE)
handlerAdapterDef getProperty values () add("contentNegotiationManager", contentNegotiationManager)
handlerAdapterDef. getPropertyValues () add(" webBindingInitializer, bindingDef)
handlerAdapterDef, getPropertyvalues () add("message Converters", message Converters)
私有方法: getMessageConverters
private ManagedList> getMessage Converters(Element element, Object source ParserContext parserContext)
Element convertersELement= DomUtils getchi l dELementBy Tb gName (element, "message-converters")
ManagedList super Objects messageConverters new ManagedListcob ject>()
千(c
onvertersElement
e
message Converters, set Source(source)
for (Element beanElement: DomUtils getchi l dEl ement sBy TagName(convertersElement,"bean","ref"))t
objectobject parserContext, getDelegate (). parseProperty SubELement (beanElement, null):
message Converters, add(object)
if (convertersElement = null I Boolean. val uef(convertersElement. getAttribute("register-defaults")))t
message Converters, set Source(source)
message converters. add (create Converterdefinition(bytearray Httpmessage Converter class source ))
Root BeanDefinition string ConverterDef create ConverterDefinition(StringHt tpMessageConverter. class, source)
stringConverterDef. getPropertyValues (). add(" writeAcceptcharset", false
messageConverters, add (stringconverterDef)
messageconverters,add(createconverterdetinition(resoUrcehttpmessageConverterclasSsource
messageconverters,add(createConverterdefinition(sourcehttpmEssageconverter.classsource))
messageconverters,add(createConverterdefinition(allencompaSsingformhttpmessageconverter.classsource))
if (represent)t
messageconverters. add (create Converterdefinition(atomfeedhTtpmessage converter. class source))
2essageconverters, add (createconverterdefinition(rSschanne lhtTpmessage conVerter, class source))i
if (jaxb2Present)t
messageconverters,addcreateConverterdefinition(jaxb2rOotelementhttpmessageConverterclasssource));
if( Jackson.2 Present】
message Converters, add (create ConverterDefinition(Mapping Jackson 2Ht tpMessageConverter class, source)
else 1f gacksonPresent
message Converters, add(create ConverterDefinition(
org,springframework.http.converterjsonMappingjacksonhttpmessAgeconverter,classsource))i
从代码中我们可以, RequestMappingHandlerAdapter设置 message Converters的逻辑
如果
Kmvc: message-converters>
Beanclass=org.exampleMyhttpmEssageconverteR/>
K/mvc: message-converters>
2. message- converters子节点不存在或它的属性 register- defaults为true的话,加入其他的转换器:
BytearraylttpmEssageConverterStringhttpmessageconVerter,ResourcelttpmessageconverterAf
我们看到这么一段:
f【 represent】
messageConvertersadd(createconverterdefinition(atoMFeedhttpmessageConverterclasssource))
message Converters, add (createconverterdefinition(rss channe LhTtpmessageconVerter, class source)):
If (78xb pResent) t
86
message Converters. add (createConverterDefinition(Jaxb 2RootELementHt tp Message Converter cLass, source)):
if (jackson2Present)[
messageConvertersadd(createconverterdefiniTion(mappingjackson2httpmesSageConverterclasssource)):
else if (jacksonPresent) i
message Converters, add(createConverterDefinition(
orgspringframework.http,converterjsonMappingjacksonhttpmessageConverterclasSsource))
这些 boolean属性是哪里来的呢,它们是 Annotationdriven arser的静态变量
cLass AnnotationDriven BeanDefinitionParser implements BeanDefinitionParser i
private static final boolean javaxValidati onPresent ClassUtils, i pResent
javax. validation Validator", AnnotationDrivenBeanDefinitionParser, class, getclassLoader())
private static finaL booLean Jaxb 2Present
CLassUtils. i sPresent("javax.xmL. bind Binder, AnnotationDrivenBe anDefinition Parser class. getcLassLoader():
private static final boolean jackson2Present
140
ClassUtils isPresent("com. fasterxml jackson databind objectMapper", AnnotationDrivenBe anDefinitionParser, class. getclassLoad
Classutils, isPresent ("com. fasterxmL jackson, core, JsonGenerator, AnnotationDri venBeanDefinitionParser, class, getclas
private static final boolean jacksonPresent
Classutils, i sPresent (org. codehaus. jackson, map, Ob jectMapper", AnnotationDrivenBeanDefinitionParser, class, get classLoader()
Classutils, I sPresent ("org. codehaus, jackson, ]sonGene rator AnnotationDrivenBeanDefinitionParser, class, get classload
146
private static boo lean romePresent
ClassUtils isPresent ("com. sun, syndication feed. wireFeed", AnnotationDriven BeanDefinitionParser class getclassLoader()
其中 Classutils中的 isPresent方法如下
310 9 public static boolean isPresent (String className, CLassLoader classLoader)i
try t
forName(className classloader)
return true
catch (Throwable ex)I
/ Class or one of its dependencies is not present
turn fal
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.