一.背景

由于项目中要用到将Java对象转为xml返回给调用者。选择使用JAXB,由于它是JDK自带的。不须要引入其它Jar包

它提供了高速而简便的方法将xml和对象互转的方法。

二.重要Class和Interface:

JAXBContext:应用的入口。用于管理XML/Java绑定信息。

Marshaller:将Java对象序列化为XML数据。

Unmarshaller:将XML数据反序列化为Java对象。

JDK中JAXB相关的重要Annotation:



三.重要的Annotation:

@XmlType,将Java类或枚举类型映射到XML模式类型

@XmlAccessorType  定义映射这个类中的何种类型须要映射到XML。可接收四个參数,各自是:

XmlAccessType.FIELD:映射这个类中的全部字段到XML

XmlAccessType.PROPERTY:映射这个类中的属性(get/set方法)到XML

XmlAccessType.PUBLIC_MEMBER:将这个类中的全部public的field或property同一时候映射到XML(默认)

XmlAccessType.NONE:不映射

@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序。

@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法)。以序列化Java类为XML。

@XmlElementWrapper ,对于数组或集合(即包括多个元素的成员变量)。生成一个包装该数组或集合的XML元素(称为包装器)。

@XmlRootElement。将Java类或枚举类型映射到XML元素。

@XmlElement。将Java类的一个属性映射到与属性同名的一个XML元素。

@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。

四.代码实现:

1. 代码结构图

2. spring配置:

  1. <?xml version="1.0" encoding="UTF-8"?
  2.  
  3. >
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. xsi:schemaLocation="http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd">
  14.  
  15. <!-- 加入注解驱动 -->
  16. <mvc:annotation-driven />
  17.  
  18. <!-- 默认扫描的包路径 -->
  19. <context:component-scan base-package="com.zdp" />
  20.  
  21. <!-- 视图解析器 -->
  22. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
  23. <property name="order" value="1"/>
  24. </bean>
  25.  
  26. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  27. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  28. </bean>
  29.  
  30. <bean name="jaxb2MarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
  31. <constructor-arg>
  32. <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  33. <property name="classesToBeBound">
  34. <array>
  35. <value>com.zdp.domain.User</value>
  36. <value>com.zdp.domain.ListBean</value>
  37. <value>com.zdp.domain.MapBean</value>
  38. </array>
  39. </property>
  40. </bean>
  41. </constructor-arg>
  42. </bean>
  43.  
  44. </beans>

3. UserBean (ListBean及MapBean请在源代码中查看)

  1. @XmlRootElement(name = "user")
  2. @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
  3. public class User {
  4. @XmlAttribute(name = "id")
  5. public String id;
  6.  
  7. @XmlAttribute(name = "name")
  8. public String name;
  9.  
  10. @XmlAttribute(name = "age")
  11. public int age;
  12.  
  13. @XmlAttribute(name = "sex")
  14. public String sex;
  15.  
  16. @XmlElement(name = "address")
  17. public String address;
  18.  
  19. @XmlElement(name = "Account")
  20. public Account account;
  21.  
  22. public static class Account {
  23. @XmlAttribute(name = "username")
  24. public String username;
  25.  
  26. @XmlValue
  27. public String password;
  28.  
  29. public Account() {
  30. }
  31.  
  32. public Account(String username, String password) {
  33. this.username = username;
  34. this.password = password;
  35. }
  36. }
  37.  
  38. @XmlElement(name = "Cards")
  39. public Cards cards;
  40.  
  41. public static class Cards {
  42. @XmlElement(name = "card")
  43. public List<String> cards;
  44.  
  45. public Cards() {
  46. }
  47.  
  48. public Cards(List<String> cards) {
  49. this.cards = cards;
  50. }
  51. }
  52.  
  53. public User(){}
  54.  
  55. }

4. Controller

  1. @Controller
  2. public class JAXBController {
  3. /**
  4. * 将对象转为xml
  5. */
  6. @RequestMapping("/object2xml")
  7. public ModelAndView object2xml(){
  8. ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
  9. User user = new User();
  10. user.name = "zhangsan";
  11. user.id = "1";
  12. user.address = "shenzhen";
  13. user.age = 20;
  14. user.sex = "man";
  15.  
  16. user.account = new Account("zhang", "abc123");
  17.  
  18. List<String> cards = new ArrayList<String>();
  19. cards.add("gonghang");
  20. cards.add("jianhang");
  21. user.cards = new Cards(cards);
  22.  
  23. mav.addObject(user);
  24. return mav;
  25. }
  26.  
  27. /**
  28. * 将list转为xml
  29. */
  30. @RequestMapping("/list2xml")
  31. public ModelAndView list2xml(){
  32. ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
  33. List<User> userList = new ArrayList<User>();
  34. for(int i = 0; i < 2; i++){
  35. User user = new User();
  36. user.name = "zhangsan" + i;
  37. user.id = "1";
  38. user.address = "shenzhen";
  39. user.age = 20;
  40. user.sex = "man";
  41. user.account = new Account("zhang" + i, "abc123");
  42. List<String> cards = new ArrayList<String>();
  43. cards.add("gonghang" + i);
  44. cards.add("jianhang" + i);
  45. user.cards = new Cards(cards);
  46.  
  47. userList.add(user);
  48. }
  49.  
  50. ListBean listBean = new ListBean();
  51. listBean.setList(userList);
  52. mav.addObject(listBean);
  53. return mav;
  54. }
  55.  
  56. /**
  57. * 将map转为xml
  58. */
  59. @RequestMapping("/map2xml")
  60. public ModelAndView map2xml(){
  61. ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
  62. MapBean mapBean = new MapBean();
  63. HashMap<String, User> map = new HashMap<String, User>();
  64.  
  65. for(int i = 0; i < 2; i++){
  66. User user = new User();
  67. user.name = "zhangsan" + i;
  68. user.id = "1";
  69. user.address = "shenzhen";
  70. user.age = 20;
  71. user.sex = "man";
  72. user.account = new Account("zhang" + i, "abc123");
  73. List<String> cards = new ArrayList<String>();
  74. cards.add("gonghang" + i);
  75. cards.add("jianhang" + i);
  76. user.cards = new Cards(cards);
  77.  
  78. map.put("1", user);
  79. }
  80.  
  81. mapBean.setMap(map);
  82. mav.addObject(mapBean);
  83. return mav;
  84. }
  85. }

5. 測试:浏览器输入http://localhost/spring_jaxb/object2xml

  1. <?
  2.  
  3. xml version="1.0" encoding="UTF-8"?
  4.  
  5. >
  6. <user sex="man" age="20" name="zhangsan" id="1">
  7. <address>shenzhen</address>
  8. <Account username="zhang">abc123</Account>
  9. <Cards>
  10. <card>gonghang</card>
  11. <card>jianhang</card>
  12. </Cards>
  13. </user>

6. 源代码:http://download.csdn.net/detail/zdp072/8074493

springMVC整合JAXB的更多相关文章

  1. (转)Dubbo与Zookeeper、SpringMVC整合和使用

    原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...

  2. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  3. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  4. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...

  6. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  7. Springmvc整合tiles框架简单入门示例(maven)

    Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...

  8. SpringMVC整合Tiles框架

    SpringMVC整合Tiles框架 Tiles组件 tiles-iconfig.xml Tiles是一个JSP布局框架. Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离 ...

  9. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)转

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

随机推荐

  1. 将markdown格式转化为bootstrap风格html

    前言:这些年markdown格式的文件很流行,像github里project说明文档都是用markdown格式编写. 一方面,我们能够通过pandoc将markdown文件转换为html,这样将htm ...

  2. CorePlot学习

    阅读这篇文章,指出它在国外    原文地址:https://github.com/core-plot/core-plot/wiki/High-Level-Design-Overview 强烈推荐阅读该 ...

  3. C 编程最佳实践(书写风格)

    简介本文是为了满足开发人员的需要而写的.我们总结了一套指南,无论作为开发人员还是顾问,这些指南多年来一直都很好地指导着我们,我们把它们作为建议提供给您,希望对您的工作有所帮助.您也许不赞同其中的某些指 ...

  4. Java模拟POST表单提交HttpClient操作

    public static void Login() { String url = "http://www.***.com/login"; PostMethod postMetho ...

  5. thinkPHP 模板中的语法知识 详细介绍(十二)

    原文:thinkPHP 模板中的语法知识 详细介绍(十二) 本章节:介绍模板中的语法,详细的语法介绍 一.导入CSS和JS文件    ==>记住常量的是大写 1.css link .js  sc ...

  6. linux内核函数之 blk_plug

    分析: /* * blk_plug permits building a queue of related requests by holding the I/O * fragments for a ...

  7. poj 3270 更换使用

    1.确定初始和目标状态. 明确.目标状态的排序状态. 2.得出置换群,.比如,数字是8 4 5 3 2 7,目标状态是2 3 4 5 7 8.能写为两个循环:(8 2 7)(4 3 5). 3.观察当 ...

  8. ssh 即使主机,同nohup背景脚本

    下面的脚本工具:先从本地副本的脚本到远程主机,然后ssh即使在远程主机,脚本的运行副本前(因为脚本需要运行很长,它运行在后台),该脚本仅用于备忘录,如果请指点不足! #!/bin/bash cd /t ...

  9. 金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on Xanga

    金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on X ...

  10. HDU 4028 The time of a day STL 模拟题

    暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vecto ...