8

I'm trying to use wsdl:fault, but can not generate expected java class (exception). The class I get generated (annotations and getters/setters removed):

public class ProjectException extends Exception {
    private com.home.project.generated.Fault fault;
}

public class Fault {
    protected String errorMessage;
    protected long errorCode;
}

The class I expect to get generated:

public class ProjectException extends Exception {
    protected String errorMessage;
    protected long errorCode;
}

My wsdl:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="ProjectSoapServiceImplService"
                  targetNamespace="http://www.home.com/webservices/v1_0/project/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:tns="http://www.home.com/webservices/v1_0/project/"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
    <wsdl:types>
        <xs:schema xmlns:tns="http://www.home.com/webservices/v1_0/project/"
                   xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"
                   targetNamespace="http://www.home.com/webservices/v1_0/project/" version="1.0">

            <xs:element name="createProject" type="tns:projectRequest"/>
            <xs:element name="projectResponse" type="tns:projectResponse"/>

            <xs:complexType name="projectRequest">
                <xs:sequence>
                    <xs:element minOccurs="0" name="projectName" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="projectResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="projectId" type="xs:long"/>
                </xs:sequence>
            </xs:complexType>
            <xs:element name="fault">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="errorMessage" type="xs:string"/>
                        <xs:element name="errorCode" type="xs:long"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>

    </wsdl:types>
    <wsdl:message name="createProject">
        <wsdl:part name="parameters" element="tns:createProject"/>
    </wsdl:message>
    <wsdl:message name="createProjectResponse">
        <wsdl:part name="parameters" element="tns:projectResponse">
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="projectException">
        <wsdl:part name="faultMessage" element="tns:fault"/>
    </wsdl:message>
    <wsdl:portType name="ProjectPort">
        <wsdl:operation name="createProject">
            <wsdl:input name="createProject" message="tns:createProject"/>
            <wsdl:output name="createProjectResponse" message="tns:createProjectResponse"/>
            <wsdl:fault name="fault" message="tns:projectException"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="ProjectSoapServiceImplServiceSoapBinding" type="tns:ProjectPort">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="createProject">
            <soap:operation soapAction="" style="document"/>
            <wsdl:input name="createProject">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="createProjectResponse">
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="fault">
                <soap:fault name="fault" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="ProjectSoapServiceImplService">
        <wsdl:port name="ProjectPortPort" binding="tns:ProjectSoapServiceImplServiceSoapBinding">
            <soap:address location="http://localhost:9090/ProjectPortPort"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

Any ideas how to inline properties in class directly?

Thanks in advance

Vi Matviichuk
  • 1,122
  • 15
  • 32

2 Answers2

3

As far as I can tell you cannot achieve to generate what you expect.

According to the specification JAX-WS 2.1 http://download.oracle.com/otndocs/jcp/jaxws-2.1-mrel2-eval-oth-JSpec/ a mapped fault needs an Exception that contains the fault bean. Using tools that implement the specification will always give you an Exception wrapper annotated with @WebFault that contain the Java Fault bean.

Mads Hoel
  • 113
  • 5
0

Please find a way to set up your web service with JAX-WS fault handing and exception handling.

According to your needs, I use @WebFault annotation :

Web service interface:

package foo.bar;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface MyProjectService {

    @WebMethod
    public String createProject(String name) throws ProjectException;
}

Web service implementation with fault :

Here, we simulate an exception according to the project name.

package foo.bar;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(serviceName="MyProjectService", portName="MyProjectServiceService")
public class MyProjectServiceImpl implements MyProjectService {

    public MyProjectServiceImpl() {
    }

    @WebMethod  
    public String createProject(@WebParam(name="name") String name) throws ProjectException {
        if (name.equalsIgnoreCase("bad name")) {
            ProjectFault fault = new ProjectFault();
            fault.setFaultCode("123");
            fault.setFaultString("Custom error message");
            throw new ProjectException("123","Custom error message");
        } else {
            return "Project created : " + name;
        }
    }
}

ProjectFault bean :

ProjectFault class is required by the JAXB runtime to process exceptions.

package foo.bar;

public class ProjectFault {

     private String faultCode;
     private String faultString;

    public String getFaultCode() {
        return faultCode;
    }

    public void setFaultCode(String faultCode) {
        this.faultCode = faultCode;
    }

    public String getFaultString() {
        return faultString;
    }

    public void setFaultString(String faultString) {
        this.faultString = faultString;
    }
}

Your custom exception : ProjectException

This class is required to be annotated with @WebFault annotation.

package foo.bar;

import javax.xml.ws.WebFault;

@WebFault(name="ProjectFault")
public class ProjectException extends Exception {

    private ProjectFault fault;

    public ProjectException() {
    }

    protected ProjectException(ProjectFault fault) {
        super(fault.getFaultString()); 
        this.fault = fault;
     }

    public ProjectException(String message, ProjectFault faultInfo){
        super(message);
        this.fault = faultInfo;
    }

    public ProjectException(String message, ProjectFault faultInfo, Throwable cause){
        super(message,cause);
        this.fault = faultInfo;
    }

    public ProjectFault getFaultInfo(){
        return fault;
    }

    public ProjectException(String message) {
        super(message);
    }

    public ProjectException(String code, String message) {
        super(message);
        this.fault = new ProjectFault();
        this.fault.setFaultString(message);
        this.fault.setFaultCode(code);
    }

    public ProjectException(Throwable cause) {
        super(cause);
    }

    public ProjectException(String message, Throwable cause) {
        super(message, cause);
    }
}

The last step is to generate your WSDL and adapt this example.

Regards, André

André Blaszczyk
  • 750
  • 4
  • 17