【问题标题】:Define WebFault in a different namespace / package than the service one (using cxf / jax-ws)在与服务不同的命名空间/包中定义 WebFault(使用 cxf / jax-ws)
【发布时间】:2012-07-12 12:44:32
【问题描述】:

我正在使用带有 cxf 实现的 jax-ws 来实现 Web 服务。
我有几个使用 @WebService 注释注释的服务。
在不同的包中,我定义了异常(从 RuntimeException 继承)并使用具有唯一命名空间的 @WebFault 注释它们。每个异常类都包含一个带有异常数据 (faultInfo) 的 bean,该异常数据 (faultInfo) 与故障位于相同的命名空间和包中,并使用 @XMlType 进行注释。
示例:
故障等级:

@WebFault(name = "GeneralRemoteFault", targetNamespace = WebServices.MyNamespace)
public class GeneralRemoteFault extends RuntimeException
{
    private GeneralRemoteFaultException faultInfo;
    public GeneralRemoteFault(String message, GeneralRemoteFaultException faultInfo) {
        super(message);
        this.faultInfo = faultInfo;
    }
    public GeneralRemoteFault(String message, GeneralRemoteFaultException faultInfo, Throwable cause) {
        super(message, cause);
        this.faultInfo = faultInfo;
    }
    public GeneralRemoteFaultException getFaultInfo() {
        return faultInfo;
    }
}

Fault Bean:(带有将其放入 WebServices.MyNamespace 命名空间的 package-info)

@XmlType(name = "GeneralRemoteFaultException ")
public class GeneralRemoteFaultException 
{
    private String objId;
    public GeneralRemoteFaultException () {}
    public GeneralRemoteFaultException (String objId)
    {
        this.objId = objId;
    }
    public String getObjId()
    {
        return objId;
    }
    public void setObjId(String objId)
    {
        this.objId = objId;
    }
}

服务方式:

List<ValidationErrors> validateObject(
        @WebParam(name = "object") ValidationObject object,
        @WebParam(name = "groups") String[] groups) throws GeneralRemoteFault;

当我运行服务器 CXF 时出现以下错误:

ERROR org.apache.cxf.service.factory.ReflectionServiceFactoryBean.fillInSchemaCrossreferences:305
[main] - Schema element {http://www.example.com/schema}ValidationRemoteFault references undefined type
{http://www.example.com/schema}ValidationRemoteFaultException for service {http://web_services.example.com/}ValidationService

在调试运行服务的代码后,我注意到模式集合 (XmlSchemaCollection.schemas) 不包含故障 bean 的命名空间,这就是它失败的原因(它只包含服务和故障那些)。 似乎 CXF 没有考虑到错误 bean 将被定义在一个单独的命名空间中而不是其他的选项,并且不会将错误 bean 模式加载到模式集合中。 即使我将故障 bean 与故障(如上定义)放在同一个命名空间中,命名空间的架构 (XmlSchema) 也不会仅包含故障的 JAXB 类型。

任何有关解决错误的见解将不胜感激。

这是弹出消息的堆栈:

at org.apache.ws.commons.schema.XmlSchemaCollection.getTypeByQName(XmlSchemaCollection.java:229)
at org.apache.cxf.common.xmlschema.SchemaCollection.getTypeByQName(SchemaCollection.java:109)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.fillInSchemaCrossreferences(ReflectionServiceFactoryBean.java:301)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:269)
  - locked <0x1670> (a org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean)
at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:205)
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)
at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:159)
at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:207)
at org.apache.cxf.jaxws.EndpointImpl.getServer(EndpointImpl.java:442)
  - locked <0x1678> (a org.apache.cxf.jaxws22.EndpointImpl)
at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:329)
at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:246)

谢谢,
阿夫纳

【问题讨论】:

  • 你能添加一些你的注释方法和故障类的代码示例吗?
  • 我已经用代码示例更新了帖子。谢谢
  • 我没有得到这个:你的错误类有 faultInfoGeneralRemoteFaultException 但你说你的错误 bean 是 ObjectNotFoundRemoteFaultException。这不是真的,你的错误 bean 是 GeneralRemoteFaultException 类。你能添加这个类的代码吗?顺便说一句......你在哪里使用 ObjectNotFoundRemoteFaultException 类?因为查看您在@WebMethod 和故障类中放入的内容,根本没有关系。我的意思是:如果你删除类 ObjectNotFoundRemoteFaultException 你的网络服务编译:-P
  • 对不起,我混错了sn-p代码。现在解决了。我今天发现,除了记录丑陋的错误消息之外,该代码实际上还可以工作。

标签: java web-services jax-ws cxf


【解决方案1】:

它应该可以工作,但尝试强制 JAXB 使用相同的命名空间,像这样设置故障 bean 的 @XmlType 注释的 namespace 属性

@XmlType(name = "GeneralRemoteFaultException ", namespace = WebServices.MyNamespace)

此外,您可以强制您的 Web 服务类使用相同的命名空间,在 @WebService 注释中设置属性 targetNamespace,如下所示:

@WebService(name = "myService", targetNamespace =  WebServices.MyNamespace)

如果这不起作用,请添加生成的 WSDL 以检查命名空间问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多