oxm-jaxb2的简单使用_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > oxm-jaxb2的简单使用

oxm-jaxb2的简单使用

 2014/7/28 15:16:15  lee372106501  程序员俱乐部  我要评论(0)
  • 摘要:操作xml有很多的工具,如果不想使用dom4j之类需要手写代码的工具,可以使用绑定工具,这类工具有Castor、Jibx、XStream、XmlBeans、Jaxb2等。本文简单的介绍spring中的Jaxb2Marshaller的使用;先写一个通用的映射类,把xml转换成对象时直接调用它的unmarshal方法即可,从对象转换成xml时只需调用它的marshal方法:packagecom.lee.test.xml;importjava.io.IOException;importjava.io
  • 标签:使用
   操作xml有很多的工具,如果不想使用dom4j之类需要手写代码的工具,可以使用绑定工具,这类工具有Castor、Jibx、XStream、XmlBeans、Jaxb2等。
    本文简单的介绍spring中的Jaxb2Marshaller的使用;
    先写一个通用的映射类,把xml转换成对象时直接调用它的unmarshal方法即可,从对象转换成xml时只需调用它的marshal方法:
class="java" name="code">
package com.lee.test.xml;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

public class Mapper {

	private Marshaller marshaller;
	private Unmarshaller unmarshaller;
	public void setMarshaller(Marshaller marshaller) {
		this.marshaller = marshaller;
	}
	public void setUnmarshaller(Unmarshaller unmarshaller) {
		this.unmarshaller = unmarshaller;
	}
	//java object --> xml
	public String marshal(Object obj) {
		String xml;
		StringWriter writer = null;
		try {
			writer = new StringWriter();
			this.marshaller.marshal(obj, new StreamResult(writer));
			xml = writer.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally{
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return xml;
	}
	//xml --> java object
	public Object unmarshal(String xml) {
		Object obj = null;
		StringReader reader = null;
		try {
			reader = new StringReader(xml);
			obj = this.unmarshaller.unmarshal(new StreamSource(reader));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally{
			if (reader != null) {
				reader.close();
			}
		}
		return obj;
	}
}


spring的配置如下:
<bean id="mapper" class="com.lee.test.xml.Mapper">
		<property name="marshaller" ref="marshaller" />
		<property name="unmarshaller" ref="marshaller" />
	</bean>
	<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.lee.test.obj.Order</value>
            </list>
        </property>
    </bean>

这里只绑定了一个类Order,如需映射其他类,只需在<list></list>里加上<value>你定义的类</value>,其他代码无需改动。
当然你也可以定义转换后xml的格式、有无声明、编码等,如下:
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.lee.test.obj.Order</value>
            </list>
        </property>
        <property name="marshallerProperties">
        	<map>
        		<!-- format : 默认 Boolean.false(Compact format)  -->
        		<entry key="jaxb.formatted.output">
        			<value type="java.lang.Boolean">true</value>
				</entry>
				<!-- xml Declaration :默认 Boolean.false -->
        		<entry key="jaxb.fragment">
        			<value type="java.lang.Boolean">false</value>
				</entry>
				<!-- xml encoding declaration: 默认 UTF-8 -->
        		<entry key="jaxb.encoding" value="GBK"/>
        	</map>
        </property>
    </bean>


这里用的是Jaxb2Marshaller,如果要使用其他的绑定工具如Castor,需要把Jaxb2Marshaller改为CastorMarshaller,Mapper类无需改动:
<bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller">
    	<property name="mappingLocation" value="classpath:mapping.xml"></property>
    </bean>


Order类的完整代码如下:
package com.lee.test.obj;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Order")
public class Order extends BaseModel{
	private String id;
	private User user;
	@XmlElement(name="Id")//不加时默认为@XmlElement(name="id")
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}

package com.lee.test.obj;

public class User extends BaseModel{
	private String name;
	private String sex;
	private String birthday;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	
}

package com.lee.test.obj;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class BaseModel {
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}


对于jaxb2的各个注解的使用请查看相关资料,如https://jaxb.java.net/tutorial/index.html,这里就不多说了。

假如有这么一个场景:客户端post一个xml报文,你经过处理后返回一个xml报文,代码如下:
package com.lee.test.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lee.test.obj.Order;
import com.lee.test.obj.User;
import com.lee.test.xml.Mapper;

@Controller
public class Home {
	@Resource
	private Mapper mapper;
	
	@RequestMapping(value="postXml", method=RequestMethod.POST)
	@ResponseBody
	public String testPostXml(@RequestBody String xml) {
		System.out.println("请求xml报文:"+xml);
		//把xml转换成java对象
		Order order = (Order)mapper.unmarshal(xml);
		
		//模拟其他操作
		order.setId("123");
		User user = order.getUser();
		user.setName("zhangsan");
		user.setBirthday("2013-12-13");
		
		//把java对象转换成xml,你可以绑定其他类,这里为了简便用了Order
		xml = mapper.marshal(order);
		System.out.println("响应xml报文:"+xml);
		return xml;
	}
}



测试结果:

  • 大小: 265.1 KB
  • 查看图片附件
发表评论
用户名: 匿名