wsdl:如何使用errorCode和errorMessage内联生成异常?

时间:2022-10-02 17:19:35

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):

我正在尝试使用wsdl:fault,但无法生成预期的java类(异常)。我生成的类(删除了注释和getter / setter):

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:

我的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

提前致谢

2 个解决方案

#1


2  

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.

根据规范JAX-WS 2.1 http://download.oracle.com/otndocs/jcp/jaxws-2.1-mrel2-eval-oth-JSpec/,映射的故障需要包含故障bean的异常。使用实现规范的工具将始终为您提供一个使用@WebFault注释的包含Java Fault bean的Exception包装器。

#2


0  

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

请找到一种使用JAX-WS故障处理和异常处理来设置Web服务的方法。

According to your needs, I use @WebFault annotation :

根据您的需要,我使用@WebFault注释:

Web service interface:

Web服务接口:

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 :

有故障的Web服务实现:

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 bean:

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

JAXB运行时需要ProjectFault类来处理异常。

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

您的自定义异常:ProjectException

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

此类需要使用@WebFault批注进行批注。

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.

最后一步是生成WSDL并调整此示例。

Regards, André

此致,安德烈

#1


2  

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.

根据规范JAX-WS 2.1 http://download.oracle.com/otndocs/jcp/jaxws-2.1-mrel2-eval-oth-JSpec/,映射的故障需要包含故障bean的异常。使用实现规范的工具将始终为您提供一个使用@WebFault注释的包含Java Fault bean的Exception包装器。

#2


0  

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

请找到一种使用JAX-WS故障处理和异常处理来设置Web服务的方法。

According to your needs, I use @WebFault annotation :

根据您的需要,我使用@WebFault注释:

Web service interface:

Web服务接口:

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 :

有故障的Web服务实现:

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 bean:

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

JAXB运行时需要ProjectFault类来处理异常。

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

您的自定义异常:ProjectException

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

此类需要使用@WebFault批注进行批注。

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.

最后一步是生成WSDL并调整此示例。

Regards, André

此致,安德烈