Jax-ws:如何区分未发送日期字段&日期格式不正确?

时间:2017-06-21 08:27:49

标签: web-services soap jax-ws

我正在使用Jax-ws创建SOAP Web服务。

我想从请求中收到错误的日期格式时发送soap fault错误。

但是,当格式不正确时,我总是收到一个空值,而我无法区分不正确的日期格式&价值未从请求中发送...

我尝试创建一个Date XmlAdapter并在那里抛出异常, 但它似乎被某个地方抓住了......

我看过类似帖子,例如:Jax ws- xs:date format validation

但它已经过时了(~4岁)而没有回答......

感谢您的帮助

编辑:

DateAdapter.java :抛出异常的适配器类

@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);
    }

}

DocumentService.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;
    }
}

CustomDocument.java :包含验证日期的架构类

{{1}}

1 个答案:

答案 0 :(得分:0)

尝试

@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;

您也可以在类型级别执行此操作。请参阅上面的网址。