Spring MVC 学习笔记 十三 xml格式输入输出
Xml格式输入
Spring mvc中缺省提供了SourceHttpMessageConverter 和 Jaxb2RootElementHttpMessageConverter,可用来解析request body中输入的xml string。
其中 SourceHttpMessageConverter 将输入的xmlString 转换成xml的Source对象(如DomSource,SaxSource等),再在handlerMethod对输入的Source对象进行后续解析。
例如
而Jaxb2RootElementHttpMessageConverter则通过JAXBinger机制将输入的xmlString直接转换成POJO对象。
例如
先定义带JAXB annotation的pojo bean
在定义handler method如下
这里spring mvc直接将输入的xml string 转换成为 BeanJaxA这个bean对象。
Xml格式输出
对于xml输出,Spring mvc提供了MarshallingView来提供对xml输出的支持。例如,在servlet context xml配置文件中增加如下配置。
以上定义使用jaxbMarshall来将java bean 转换为xml string并利用ContentNegotiatingViewResolver来提供输出。
可访问测试工程中http://localhost:8080/springmvc/中新增的
“xml input jaxb test” 和 “xml input domsource test2”两个按钮来查看xml 数据输入输出的效果。
Spring mvc中缺省提供了SourceHttpMessageConverter 和 Jaxb2RootElementHttpMessageConverter,可用来解析request body中输入的xml string。
其中 SourceHttpMessageConverter 将输入的xmlString 转换成xml的Source对象(如DomSource,SaxSource等),再在handlerMethod对输入的Source对象进行后续解析。
例如
- @RequestMapping("/xml2.xml")
- public BeanJaxbA testXML2(@RequestBody DOMSource a){
- try {
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- transformer.transform(a, result);
- System.out.println(writer.toString()) ;
- } catch (TransformerConfigurationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (TransformerFactoryConfigurationError e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (TransformerException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
@RequestMapping("/xml2.xml") public BeanJaxbA testXML2(@RequestBody DOMSource a){ try { StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(a, result); System.out.println(writer.toString()) ; } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); }
而Jaxb2RootElementHttpMessageConverter则通过JAXBinger机制将输入的xmlString直接转换成POJO对象。
例如
先定义带JAXB annotation的pojo bean
- @XmlRootElement(name="xmlStrA")
- public class BeanJaxbA {
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- private String name;
- private String password;
- private long id;
- }
@XmlRootElement(name="xmlStrA") public class BeanJaxbA { public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public long getId() { return id; } public void setId(long id) { this.id = id; } private String name; private String password; private long id; }
在定义handler method如下
- @RequestMapping("/xml.xml")
- public BeanJaxbA testXML1(@RequestBody BeanJaxbA a){
- BeanJaxbA a1 = new BeanJaxbA();
- a1.setId(2000);
- a1.setName("winzip");
- a1.setPassword("password");
- return a1;
- }
@RequestMapping("/xml.xml") public BeanJaxbA testXML1(@RequestBody BeanJaxbA a){ BeanJaxbA a1 = new BeanJaxbA(); a1.setId(2000); a1.setName("winzip"); a1.setPassword("password"); return a1; }
这里spring mvc直接将输入的xml string 转换成为 BeanJaxA这个bean对象。
Xml格式输出
对于xml输出,Spring mvc提供了MarshallingView来提供对xml输出的支持。例如,在servlet context xml配置文件中增加如下配置。
- <bean
- class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
- <property name="order" value="1" />
- <property name="mediaTypes">
- <map>
- <entry key="json" value="application/json" />
- <entry key="xml" value="application/xml" /> <!-- SPR-7504 -->
- </map>
- </property>
- <property name="defaultViews">
- <list>
- <bean class="net.zhepu.json.MappingJacksonJsonView" />
- <bean name="jaxb2MarshallingView"
- class="org.springframework.web.servlet.view.xml.MarshallingView">
- <property name="marshaller" ref="jaxbMarshall"></property>
- </bean>
- </list>
- </property>
- <property name="ignoreAcceptHeader" value="true" />
- </bean>
- <oxm:jaxb2-marshaller id="jaxbMarshall">
- <oxm:class-to-be-bound name="net.zhepu.web.xmlModel.BeanJaxbA" />
- </oxm:jaxb2-marshaller>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1" /> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> <!-- SPR-7504 --> </map> </property> <property name="defaultViews"> <list> <bean class="net.zhepu.json.MappingJacksonJsonView" /> <bean name="jaxb2MarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <property name="marshaller" ref="jaxbMarshall"></property> </bean> </list> </property> <property name="ignoreAcceptHeader" value="true" /> </bean> <oxm:jaxb2-marshaller id="jaxbMarshall"> <oxm:class-to-be-bound name="net.zhepu.web.xmlModel.BeanJaxbA" /> </oxm:jaxb2-marshaller>
以上定义使用jaxbMarshall来将java bean 转换为xml string并利用ContentNegotiatingViewResolver来提供输出。
可访问测试工程中http://localhost:8080/springmvc/中新增的
“xml input jaxb test” 和 “xml input domsource test2”两个按钮来查看xml 数据输入输出的效果。