cxf webservice接收date类型字段值为空时,后台会报错。原因是cxf没有很好处理空值的情况。
解决方案
自定义date字段的转换方式。以springboot cxf 为例。涉及以下三点改造。
1、增加自定义date转换类
public class DateXmlAdapter extends XmlAdapter<String,Date>{
@Override
public Date unmarshal(String v) throws Exception {
if(StringUtil.isBlank(v)) {
return null;
}
return DateUtil.parseDate(v);
}
@Override
public String marshal(Date v) throws Exception {
if(null == v) { return "";}
return DateUtil.formatDate(v, "yyyy-MM-dd");
}
}
2、对象class增加注解
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
class Person{
3、属性增加注解
@XmlSchemaType(name = "string", type = Date.class)
@XmlJavaTypeAdapter(value = DateXmlAdapter.class)
private Date sdate;