`

修改cxf rest的返回时间格式

 
阅读更多

参考:http://stackoverflow.com/questions/2519432/jaxb-unmarshal-timestamp

 

 

cxf rest默认使用2010-08-23T20:32:43.609+08:00这种时间格式,我们可以通过定义annotation来改变它。
新建annotation:

 

import java.text.SimpleDateFormat; 
import java.util.Date; 
 
import javax.xml.bind.annotation.adapters.XmlAdapter; 
 
public class DateAdapter extends XmlAdapter<String, Date> { 
 
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
 
    @Override 
    public String marshal(Date v) throws Exception { 
        return dateFormat.format(v); 
    } 
 
    @Override 
    public Date unmarshal(String v) throws Exception { 
        return dateFormat.parse(v); 
    } 
} 

 

 

@XmlRootElement(name = "resp")
@XmlAccessorType( XmlAccessType.FIELD )
public class BaseRespObj {
	@XmlJavaTypeAdapter(DateAdapter.class)
	private Date date;
	@XmlElement(name = "infocode")
	private ResultCode code;

 

 

将注解@XmlJavaTypeAdapter(DateAdapter.class)放在时间属性上即可。此时时间将返回2010-08-23 20:32:43格式。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics