Spring MVC 学习笔记 十三 xml格式输入输出

来源:互联网 发布:mac 人民币符号 编辑:程序博客网 时间:2024/06/06 02:50
Xml格式输入 
       Spring mvc中缺省提供了SourceHttpMessageConverter 和 Jaxb2RootElementHttpMessageConverter,可用来解析request body中输入的xml string。 
其中 SourceHttpMessageConverter 将输入的xmlString 转换成xml的Source对象(如DomSource,SaxSource等),再在handlerMethod对输入的Source对象进行后续解析。 
例如 
Java代码
  1. @RequestMapping("/xml2.xml")  
  2.     public BeanJaxbA testXML2(@RequestBody DOMSource a){  
  3.   
  4.           try {  
  5.             StringWriter writer = new StringWriter();  
  6.               StreamResult result = new StreamResult(writer);  
  7.               TransformerFactory tf = TransformerFactory.newInstance();  
  8.               Transformer transformer = tf.newTransformer();  
  9.               transformer.transform(a, result);  
  10.               System.out.println(writer.toString()) ;  
  11.         } catch (TransformerConfigurationException e) {  
  12.             // TODO Auto-generated catch block  
  13.             e.printStackTrace();  
  14.         } catch (TransformerFactoryConfigurationError e) {  
  15.             // TODO Auto-generated catch block  
  16.             e.printStackTrace();  
  17.         } catch (TransformerException e) {  
  18.             // TODO Auto-generated catch block  
  19.             e.printStackTrace();  
  20.         }   
而Jaxb2RootElementHttpMessageConverter则通过JAXBinger机制将输入的xmlString直接转换成POJO对象。 
例如 
先定义带JAXB  annotation的pojo bean 
Java代码
  1. @XmlRootElement(name="xmlStrA")  
  2. public class BeanJaxbA {  
  3.     public String getName() {  
  4.         return name;  
  5.     }  
  6.   
  7.     public void setName(String name) {  
  8.         this.name = name;  
  9.     }  
  10.   
  11.     public String getPassword() {  
  12.         return password;  
  13.     }  
  14.   
  15.     public void setPassword(String password) {  
  16.         this.password = password;  
  17.     }  
  18.   
  19.     public long getId() {  
  20.         return id;  
  21.     }  
  22.   
  23.     public void setId(long id) {  
  24.         this.id = id;  
  25.     }  
  26.   
  27.     private String name;  
  28.     private String password;  
  29.     private long id;  
  30.   
  31. }  
在定义handler method如下 
Java代码
  1. @RequestMapping("/xml.xml")  
  2.  public BeanJaxbA testXML1(@RequestBody BeanJaxbA a){  
  3.     BeanJaxbA a1 = new BeanJaxbA();  
  4.     a1.setId(2000);  
  5.     a1.setName("winzip");  
  6.     a1.setPassword("password");  
  7.       
  8.     return a1;  
  9.  }  
这里spring mvc直接将输入的xml string 转换成为 BeanJaxA这个bean对象。 
Xml格式输出 
对于xml输出,Spring mvc提供了MarshallingView来提供对xml输出的支持。例如,在servlet context xml配置文件中增加如下配置。 
Xml代码
  1. <bean  
  2.     class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
  3.     <property name="order" value="1" />  
  4.     <property name="mediaTypes">  
  5.         <map>  
  6.             <entry key="json" value="application/json" />  
  7.             <entry key="xml" value="application/xml" /> <!-- SPR-7504 -->  
  8.         </map>  
  9.     </property>  
  10.     <property name="defaultViews">  
  11.         <list>  
  12.             <bean class="net.zhepu.json.MappingJacksonJsonView" />  
  13.   
  14.             <bean name="jaxb2MarshallingView"  
  15.                 class="org.springframework.web.servlet.view.xml.MarshallingView">  
  16.                 <property name="marshaller" ref="jaxbMarshall"></property>  
  17.             </bean>  
  18.         </list>  
  19.     </property>  
  20.     <property name="ignoreAcceptHeader" value="true" />  
  21. </bean>  
  22. <oxm:jaxb2-marshaller id="jaxbMarshall">  
  23.     <oxm:class-to-be-bound name="net.zhepu.web.xmlModel.BeanJaxbA" />  
  24. </oxm:jaxb2-marshaller>  
以上定义使用jaxbMarshall来将java bean 转换为xml string并利用ContentNegotiatingViewResolver来提供输出。 
可访问测试工程中http://localhost:8080/springmvc/中新增的 “xml input jaxb test” 和 “xml input domsource test2”两个按钮来查看xml 数据输入输出的效果。
0 0
原创粉丝点击