码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC整合JAXB

时间:2015-12-14 16:21:47      阅读:1986      评论:0      收藏:0      [点我收藏+]

标签:

一.背景

由于项目中要用到将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:将这个类中的全部publicfieldproperty同一时候映射到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

标签:

原文地址:http://www.cnblogs.com/yxwkf/p/5045426.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!