jaxb util_JAXB自定义绑定– Java.util.Date / Spring 3序列化
jaxb util
JaxB可以處理Java.util.Date序列化,但是需要以下格式: “ yyyy-MM-ddTHH:mm:ss ”。 如果需要將日期對象格式化為另一種格式怎么辦?我有同樣的問題時,我正在同春MVC 3和Jackson JSON處理器 ,最近,我遇到了同樣的問題與Spring MVC 3和工作JAXB用于XML序列化 。
讓我們來探討這個問題:
問題:
我有以下Java Bean,要使用Spring MVC 3以XML進行序列化:
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我還有另一個對象將上面的POJO包裝起來:
package com.loiane.model;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控制器中,我將通過@ResponseBody批注返回一個公司列表-這將使用JaxB自動序列化該對象:
@RequestMapping(value="/company/view.action") public @ResponseBody Companies view() throws Exception {}當我調用controller方法時,這就是返回視圖的內容:
<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>注意日期格式。 它不是我希望它返回的格式。 我需要以以下格式序列化日期:“ MM-dd-yyyy ” 解決方案:
我需要創建一個擴展XmlAdapter的類并重寫marshal和unmarshal方法,在這些方法中,我將根據需要設置日期格式:
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");@Overridepublic String marshal(Date date) throws Exception {return dateFormat.format(date);}@Overridepublic Date unmarshal(String date) throws Exception {return dateFormat.parse(date);} }在我的Java Bean類中,我只需要在date屬性的get方法中添加@XmlJavaTypeAdapter批注。
package com.loiane.model;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 }如果我們嘗試再次調用controller方法,它將返回以下XML:
<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>問題解決了!
編碼愉快!
參考:來自Loiane Groner博客博客的JCG合作伙伴 Loiane Groner提供的JAXB自定義綁定– Java.util.Date/Spring 3序列化 。
翻譯自: https://www.javacodegeeks.com/2012/06/jaxb-custom-binding-javautildate-spring.html
jaxb util
總結
以上是生活随笔為你收集整理的jaxb util_JAXB自定义绑定– Java.util.Date / Spring 3序列化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何鉴别银行转账回执真假?
- 下一篇: 现货白银可以操作欧洲盘和美洲盘吗?