我正在使用Jax-ws创建一个SOAP Web服务。
我想在从请求中收到错误的日期格式时发送肥皂错误错误。
但是,当格式不正确时,我总是收到一个 null 值,并且我无法区分不正确的日期格式和未从请求中发送的值......
我试图创建一个日期Xml适配器并在那里抛出异常,但似乎它在某个地方被抓住了...
我看到了类似的帖子,例如:Jax ws- xs:date格式验证
但它已经过时了(~4 岁(,没有回答......
感谢您的帮助
编辑:
日期适配器.java :引发异常的适配器类
public class DateAdapter extends XmlAdapter<String, Date> {
private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
@Override
public String marshal(Date v) {
synchronized (simpleDateFormat) {
return simpleDateFormat.format(v);
}
}
@Override
public Date unmarshal(String v) throws InvalidParameterFault {
synchronized (simpleDateFormat) {
Date date = null;
try {
date = simpleDateFormat.parse(v);
return date;
} catch (ParseException e) {
throw new InvalidParameterFault("Wrong date format");
}
}
}
}
DocumentService.java:Web 服务类
@MTOM
@WebService(endpointInterface = "com.example.webservice", serviceName = "example-webservice")
@Slf4j
public class DocumentServerImpl implements DocumentServer {
private CustomService customService;
public DocumentServerImpl() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
customService = context.getBean(CustomService.class);
}
@Override
public CustomDocument downloadDocument(String id) throws InvalidParameterFault {
log.info("Download document with id <{}>", id);
return customService.load(id);
}
@Override
public String uploadDocument(CustomDocument document) throws InvalidParameterFault {
log.info("Upload document");
return customService.store(document);
}
}
自定义文档.java:包含要验证的日期的架构类
@Setter
@XmlType(name = "CustomDocument")
public class CustomDocument {
private DataHandler dataHandler;
private Date customDate;
public CustomDocument() {
}
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getCustomDate() {
return this.customDate;
}
}
尝试
@XmlRootElement
public class CustomDocument {
private DataHandler dataHandler;
private Date customDate;
public CustomDocument() {
}
@XmlAttribute
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getCustomDate() {
return this.customDate;
}
}
编辑:回复评论
从 http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html ,在包级别,尝试
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=StringAdapter.class, type=String.class)
})
package xmladapter.bar;
您也可以在类型级别执行此操作。 请参阅上面的网址。