当前位置:编程学习 > JAVA >>

jaxb绑定中的java.util.date问题

在spring mvc3中JACKSON序列化日期格式的问题(http://jackyrong.iteye.com/admin/blogs/1089909)一文中,提到了相关的日期问题。同样地,如果在JAXB中,REST形式返回的话,也有这样的问题,继续看例子:

1)POJO类:
   package com.loiane.model;

import java.util.Date;

public class Company {

    private int id;

    private String company;

    private double price;

    private double change;

    private double pctChange;

    private Date lastChange;

    //getters and setters

2)现在要返回company的集合,而且要用JAXB包装
   import java.util.List;

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

@XmlRootElement(name="companies")
public class Companies {

    @XmlElement(required = true)
    private List<Company> list;

    public void setList(List<Company> list) {
        this.list = list;
    }
}
而在SPRING CONTROLLER中,要返回如下的:
  @RequestMapping(value="/company/view.action")
public @ResponseBody Companies view() throws Exception {}

这时,返回的XML为如下:
<companies>
    <list>
        <change>0.02</change>
        <company>3m Co</company>
        <id>1</id>
        <lastChange>2011-09-01T00:00:00-03:00</lastChange>
        <pctChange>0.03</pctChange>
        <price>71.72</price>
    </list>
    <list>
        <change>0.42</change>
        <company>Alcoa Inc</company>
        <id>2</id>
        <lastChange>2011-09-01T00:00:00-03:00</lastChange>
        <pctChange>1.47</pctChange>
        <price>29.01</price>
    </list>
</companies>
可以看到,时间格式不是很好使,于是我们自定义进行格式化一下:
package com.loiane.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class JaxbDateSerializer extends XmlAdapter<String, Date>{

    private SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

    @Override
    public String marshal(Date date) throws Exception {
        return dateFormat.format(date);
    }

    @Override
    public Date unmarshal(String date) throws Exception {
        return dateFormat.parse(date);
    }
}

这里继承了XmlAdapter,并且进行了marshal和unmarshal,就是字符串日期和日期转字符,使用时,注意加上 @XmlJavaTypeAdapter 标签,如下
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.loiane.util.JaxbDateSerializer;

public class Company {

    private int id;

    private String company;

    private double price;

    private double change;

    private double pctChange;

    private Date lastChange;

    @XmlJavaTypeAdapter(JaxbDateSerializer.class)
    public Date getLastChange() {
        return lastChange;
    }
    //getters and setters
}
结果就正确输出了;
<companies>
    <list>
        <change>0.02</change>
        <company>3m Co</company>
        <id>1</id>
        <lastChange>09-01-2011</lastChange>
        <pctChange>0.03</pctChange>
        <price>71.72</price>
    </list>
    <list>
        <change>0.42</change>
        <company>Alcoa Inc</company>
        <id>2</id>
        <lastChange>09-01-2011</lastChange>
        <pctChange>1.47</pctChange>
        <price>29.01</price>
    </list>
</companies>

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,