Define WebFault in a different namespace / package than the service one (using cxf / jax-ws)

喜欢而已 提交于 2019-12-24 20:34:18

问题


I'm using jax-ws with cxf implementation to implement web services.
I have several services annotated with the @WebService annotation.
In a different package I defined the exceptions (which inherit from RuntimeException) and annotated them with @WebFault with a unique namespace. Each exception class holds a single bean with the exception data (faultInfo) which resides in the same namespace and package as the fault and is annotated with @XMlType.
Example:
Fault Class:

@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: (With package-info which puts it in WebServices.MyNamespace namespace)

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

The service method:

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

When I run the server CXF complain with the following error:

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

After debugging the code which run the service I've noticed that the schema collection (XmlSchemaCollection.schemas) doesn't include the namespace of the fault bean and that is why it fails (it contains only the service one's and the fault one's). It seems that CXF doesn't take into account the option that the fault bean will be defined in a separate namespace than the others and doesn't loads the fault bean schema into the schema collection. Even if I put the fault bean together in the same namespace with the fault (as defined above), the schema (XmlSchema) for the namespace won't include the JAXB types only the faults.

Any insight about solving the error will be appreciated.

This is the stack of where the message pops from:

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)

Thanks,
Avner


回答1:


It should work but try forcing JAXB to use the same namespace setting the namespace attribute of the @XmlType annotation of your fault bean like this

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

Also, you can force your web service class to use the same namespace setting the attribute targetNamespace in the @WebService annotation like this:

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

If this not work, please add the resulting WSDL to check the namespace issues.



来源:https://stackoverflow.com/questions/11452144/define-webfault-in-a-different-namespace-package-than-the-service-one-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!