springMVC整合JAXB
一.背景
由于项目中要用到将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的更多相关文章
- (转)Dubbo与Zookeeper、SpringMVC整合和使用
原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...
- SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- springmvc整合fastjson
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...
- 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- Springmvc整合tiles框架简单入门示例(maven)
Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...
- SpringMVC整合Tiles框架
SpringMVC整合Tiles框架 Tiles组件 tiles-iconfig.xml Tiles是一个JSP布局框架. Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离 ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)转
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
随机推荐
- VI01增强问题
函数'SD_SCD_ITEM_PRICING_DATA_GET',其实在增强中和交货相关的数据在这个函数中都可以取到,没有必要再从LIKP.LIPS等等中重新取数. include程序RV64A631 ...
- sap的示例代码
sap的示例代码查看1.在SE38环境下的程序名输入栏输入'DEMO*'后按F4,你可以查到SAP所有的DEMO示例程序,好好看看,你会学到很多ABAP功能的实现方法.2.运行“ABAPDOCU”T- ...
- 第二章 IoC Setter注入
Setter注入又称为属性注入.是通过属性的setXXX()方法来注入Bean的属性值或依赖对象.由于Setter注入具有可选择性和灵活性高的优点,因此Setter注入是实际应用中最常用的注入方式. ...
- Android入门第六篇之ListView (一)
本文来自http://blog.csdn.net/hellogv/ ListView是一个经经常使用到的控件,ListView里面的每一个子项Item能够使一个字符串,也能够是一个组合控件.先说说Li ...
- 14.5.4 InnoDB File-Per-Table Tablespaces 每个表一个文件
14.5.4 InnoDB File-Per-Table Tablespaces 每个表一个文件 从历史上看, 所有的InnoDB 表和索引是存储在system 表空间, 这个整体的方法是针对机器专注 ...
- windows下搭建node.js及npm的工作环境
近期在研究数据可视化D3框架,决定在windows下搭建一个nodejs及npm的工作环境,在网上查了n篇文章,别管是编译源代码安装也好.还是使用node.msi格式安装包也好,总是有问题.终于,功夫 ...
- [Windows Phone]模仿魔兽3技能按钮SkillButton
简介: 模仿魔兽3技能按钮,带CD效果.使用的时候可以当做普通按钮使用,同时也支持Binding. 音效紧耦合在控件内部,因为控件本身目的就是模拟魔兽3的技能按钮,所以不考虑音效的扩展. Demo结构 ...
- g++优化选项
g++优化选项 g++优化选项 对于下面的这段代码: 1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std; ...
- junit4同一时候測试多个測试类
两个分别须要的測试类 TestSuit001 package com.test.junit; import org.junit.Test; public class TestSuit001 { @Te ...
- 以JTextPanel为例Swing的鼠标事件详解
如下界面可以通过该界面研究一下Swing的鼠标事件: 图中用红粗线圈起来的为JtextPanel,该Panel添加了鼠标事件监听器,鼠标事件监听器有三种,分别为MouseWheelListener,M ...