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是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
随机推荐
- 将markdown格式转化为bootstrap风格html
前言:这些年markdown格式的文件很流行,像github里project说明文档都是用markdown格式编写. 一方面,我们能够通过pandoc将markdown文件转换为html,这样将htm ...
- CorePlot学习
阅读这篇文章,指出它在国外 原文地址:https://github.com/core-plot/core-plot/wiki/High-Level-Design-Overview 强烈推荐阅读该 ...
- C 编程最佳实践(书写风格)
简介本文是为了满足开发人员的需要而写的.我们总结了一套指南,无论作为开发人员还是顾问,这些指南多年来一直都很好地指导着我们,我们把它们作为建议提供给您,希望对您的工作有所帮助.您也许不赞同其中的某些指 ...
- Java模拟POST表单提交HttpClient操作
public static void Login() { String url = "http://www.***.com/login"; PostMethod postMetho ...
- thinkPHP 模板中的语法知识 详细介绍(十二)
原文:thinkPHP 模板中的语法知识 详细介绍(十二) 本章节:介绍模板中的语法,详细的语法介绍 一.导入CSS和JS文件 ==>记住常量的是大写 1.css link .js sc ...
- linux内核函数之 blk_plug
分析: /* * blk_plug permits building a queue of related requests by holding the I/O * fragments for a ...
- poj 3270 更换使用
1.确定初始和目标状态. 明确.目标状态的排序状态. 2.得出置换群,.比如,数字是8 4 5 3 2 7,目标状态是2 3 4 5 7 8.能写为两个循环:(8 2 7)(4 3 5). 3.观察当 ...
- ssh 即使主机,同nohup背景脚本
下面的脚本工具:先从本地副本的脚本到远程主机,然后ssh即使在远程主机,脚本的运行副本前(因为脚本需要运行很长,它运行在后台),该脚本仅用于备忘录,如果请指点不足! #!/bin/bash cd /t ...
- 金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on Xanga
金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on X ...
- HDU 4028 The time of a day STL 模拟题
暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vecto ...