一.背景

由于项目中要用到将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配置:

<?xml version="1.0" encoding="UTF-8"?

>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加入注解驱动 -->
<mvc:annotation-driven /> <!-- 默认扫描的包路径 -->
<context:component-scan base-package="com.zdp" /> <!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1"/>
</bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean> <bean name="jaxb2MarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<array>
<value>com.zdp.domain.User</value>
<value>com.zdp.domain.ListBean</value>
<value>com.zdp.domain.MapBean</value>
</array>
</property>
</bean>
</constructor-arg>
</bean> </beans>

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

@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class User {
@XmlAttribute(name = "id")
public String id; @XmlAttribute(name = "name")
public String name; @XmlAttribute(name = "age")
public int age; @XmlAttribute(name = "sex")
public String sex; @XmlElement(name = "address")
public String address; @XmlElement(name = "Account")
public Account account; public static class Account {
@XmlAttribute(name = "username")
public String username; @XmlValue
public String password; public Account() {
} public Account(String username, String password) {
this.username = username;
this.password = password;
}
} @XmlElement(name = "Cards")
public Cards cards; public static class Cards {
@XmlElement(name = "card")
public List<String> cards; public Cards() {
} public Cards(List<String> cards) {
this.cards = cards;
}
} public User(){} }

4. Controller

@Controller
public class JAXBController {
/**
* 将对象转为xml
*/
@RequestMapping("/object2xml")
public ModelAndView object2xml(){
ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
User user = new User();
user.name = "zhangsan";
user.id = "1";
user.address = "shenzhen";
user.age = 20;
user.sex = "man"; user.account = new Account("zhang", "abc123"); List<String> cards = new ArrayList<String>();
cards.add("gonghang");
cards.add("jianhang");
user.cards = new Cards(cards); mav.addObject(user);
return mav;
} /**
* 将list转为xml
*/
@RequestMapping("/list2xml")
public ModelAndView list2xml(){
ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
List<User> userList = new ArrayList<User>();
for(int i = 0; i < 2; i++){
User user = new User();
user.name = "zhangsan" + i;
user.id = "1";
user.address = "shenzhen";
user.age = 20;
user.sex = "man";
user.account = new Account("zhang" + i, "abc123");
List<String> cards = new ArrayList<String>();
cards.add("gonghang" + i);
cards.add("jianhang" + i);
user.cards = new Cards(cards); userList.add(user);
} ListBean listBean = new ListBean();
listBean.setList(userList);
mav.addObject(listBean);
return mav;
} /**
* 将map转为xml
*/
@RequestMapping("/map2xml")
public ModelAndView map2xml(){
ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
MapBean mapBean = new MapBean();
HashMap<String, User> map = new HashMap<String, User>(); for(int i = 0; i < 2; i++){
User user = new User();
user.name = "zhangsan" + i;
user.id = "1";
user.address = "shenzhen";
user.age = 20;
user.sex = "man";
user.account = new Account("zhang" + i, "abc123");
List<String> cards = new ArrayList<String>();
cards.add("gonghang" + i);
cards.add("jianhang" + i);
user.cards = new Cards(cards); map.put("1", user);
} mapBean.setMap(map);
mav.addObject(mapBean);
return mav;
}
}

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

<?

xml version="1.0" encoding="UTF-8"?

>
<user sex="man" age="20" name="zhangsan" id="1">
<address>shenzhen</address>
<Account username="zhang">abc123</Account>
<Cards>
<card>gonghang</card>
<card>jianhang</card>
</Cards>
</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. uva 1335 - Beijing Guards(二分)

    题目链接:uva 1335 - Beijing Guards 题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物, ...

  2. 动态修改PE文件图标(使用UpdateResource API函数)

    PE文件的图标存储在资源文件中,而操作资源要用到的API函数就是UpdateResource首先我们需要先了解一下ICO格式,参考资料:http://www.moon-soft.com/program ...

  3. Android菜鸟的成长笔记(13)——异步任务(Async Task)

    原文:[置顶] Android菜鸟的成长笔记(13)——异步任务(Async Task) Android的UI线程主要负责处理用户的事件及图形显示,因此主线程UI不能阻塞,否则会弹出一个ANR(App ...

  4. qt+boost::asio+tcp文件传输

    客户端: void qt_boost::pbSendFileClicked(){ QString filename = ui.leFileName->text(); QByteArray ba ...

  5. Linux vmstat命令详解

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...

  6. codeforces584B Kolya and Tanya

    题目链接:http://codeforces.com/problemset/problem/584/B 解题思路:当n=1时,_______    _______   ______  三个数每位上可以 ...

  7. 《转》python 网络编程

    原地址:http://blog.163.com/benben_long/blog/static/19945824320121225918434/ 网络客户端: 1. 理解socket: socket是 ...

  8. MySql 链接url 参数详解

    最近 整理了一下网上关于MySql 链接url 参数的设置,有不正确的地方希望大家多多指教: mysql JDBC URL格式如下: jdbc:mysql://[host:port],[host:po ...

  9. Codeforce 143B - Help Kingdom of Far Far Away 2

    B. Help Kingdom of Far Far Away 2 time limit per test 2 seconds memory limit per test 256 megabytes ...

  10. 结合使用AngularJS和Django

    原地址 好吧,我承认自己很懒,时间又不够用. 翻译的几个文章都是虎头蛇尾,但我保证这次肯定不太监. 关键的单词不翻译,实在觉得翻译成汉语很别扭,括号里是参考翻译. 有问题和建议尽管提出来,我会改进完善 ...